濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > python 如何獲取頁面所有a標(biāo)簽下href的值

python 如何獲取頁面所有a標(biāo)簽下href的值

熱門標(biāo)簽:幫人做地圖標(biāo)注收費(fèi)算詐騙嗎 外呼不封號(hào)系統(tǒng) 蘇州電銷機(jī)器人十大排行榜 遼寧400電話辦理多少錢 溫州旅游地圖標(biāo)注 悟空智電銷機(jī)器人6 荊州云電銷機(jī)器人供應(yīng)商 江蘇房產(chǎn)電銷機(jī)器人廠家 電信營業(yè)廳400電話申請(qǐng)

看代碼吧~

# -*- coding:utf-8 -*-
#python 2.7
#http://tieba.baidu.com/p/2460150866
#標(biāo)簽操作 
 
from bs4 import BeautifulSoup
import urllib.request
import re 
 
#如果是網(wǎng)址,可以用這個(gè)辦法來讀取網(wǎng)頁
#html_doc = "http://tieba.baidu.com/p/2460150866"
#req = urllib.request.Request(html_doc)  
#webpage = urllib.request.urlopen(req)  
#html = webpage.read() 
 
html="""
html>head>title>The Dormouse's story/title>/head>
body>
p class="title" name="dromouse">b>The Dormouse's story/b>/p>
p class="story">Once upon a time there were three little sisters; and their names were
a  rel="external nofollow"  rel="external nofollow"  class="sister" id="xiaodeng">!-- Elsie -->/a>,
a  rel="external nofollow"  rel="external nofollow"  class="sister" id="link2">Lacie/a> and
a  rel="external nofollow"  class="sister" id="link3">Tillie/a>;
a  rel="external nofollow"  rel="external nofollow"  class="sister" id="xiaodeng">Lacie/a>
and they lived at the bottom of a well./p>
p class="story">.../p>
"""
soup = BeautifulSoup(html, 'html.parser')   #文檔對(duì)象 
 
#查找a標(biāo)簽,只會(huì)查找出一個(gè)a標(biāo)簽
#print(soup.a)#a class="sister"  rel="external nofollow"  rel="external nofollow"  id="xiaodeng">!-- Elsie -->/a>
 
for k in soup.find_all('a'):
    print(k)
    print(k['class'])#查a標(biāo)簽的class屬性
    print(k['id'])#查a標(biāo)簽的id值
    print(k['href'])#查a標(biāo)簽的href值
    print(k.string)#查a標(biāo)簽的string 
    

如果,標(biāo)簽a>中含有其他標(biāo)簽,比如em>../em>,此時(shí)要提取a>中的數(shù)據(jù),需要用k.get_text()

soup = BeautifulSoup(html, 'html.parser')   #文檔對(duì)象
#查找a標(biāo)簽,只會(huì)查找出一個(gè)a標(biāo)簽
for k in soup.find_all('a'):
    print(k)
    print(k['class'])#查a標(biāo)簽的class屬性
    print(k['id'])#查a標(biāo)簽的id值
    print(k['href'])#查a標(biāo)簽的href值
    print(k.string)#查a標(biāo)簽的string

如果,標(biāo)簽a>中含有其他標(biāo)簽,比如em>../em>,此時(shí)要提取a>中的數(shù)據(jù),需要用k.get_text()

通常我們使用下面這種模式也是能夠處理的,下面的方法使用了get()。

 html = urlopen(url)
 soup = BeautifulSoup(html, 'html.parser')
 t1 = soup.find_all('a')
 print t1
 href_list = []
 for t2 in t1:
    t3 = t2.get('href')
    href_list.append(t3)

補(bǔ)充:python爬蟲獲取任意頁面的標(biāo)簽和屬性(包括獲取a標(biāo)簽的href屬性)

看代碼吧~

# coding=utf-8 
from bs4 import BeautifulSoup 
import requests 
# 定義一個(gè)獲取url頁面下label標(biāo)簽的attr屬性的函數(shù) 
def getHtml(url, label, attr): 
    response = requests.get(url) 
    response.encoding = 'utf-8' 
    html = response.text 
    soup = BeautifulSoup(html, 'html.parser'); 
    for target in soup.find_all(label):
 
        try: 
            value = target.get(attr)
 
        except: 
            value = ''
 
        if value: 
            print(value)
 
url = 'https://baidu.com/' 
label = 'a' 
attr = 'href' 
getHtml(url, label, attr)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • python爬蟲之異常捕獲及標(biāo)簽過濾詳解
  • Python深度學(xué)習(xí)之圖像標(biāo)簽標(biāo)注軟件labelme詳解
  • python中Tkinter實(shí)現(xiàn)分頁標(biāo)簽的示例代碼
  • Python 實(shí)現(xiàn)自動(dòng)完成A4標(biāo)簽排版打印功能
  • Python氣泡提示與標(biāo)簽的實(shí)現(xiàn)
  • Python 生成VOC格式的標(biāo)簽實(shí)例
  • 基于python3生成標(biāo)簽云代碼解析
  • python 實(shí)現(xiàn)添加標(biāo)簽&打標(biāo)簽的操作

標(biāo)簽:黃山 濟(jì)南 景德鎮(zhèn) 三沙 臺(tái)灣 喀什 欽州 宿遷

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python 如何獲取頁面所有a標(biāo)簽下href的值》,本文關(guān)鍵詞  python,如何,獲取,頁面,所有,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《python 如何獲取頁面所有a標(biāo)簽下href的值》相關(guān)的同類信息!
  • 本頁收集關(guān)于python 如何獲取頁面所有a標(biāo)簽下href的值的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    益阳市| 日照市| 巴南区| 南开区| 崇仁县| 霍山县| 抚顺市| 石柱| 秦皇岛市| 扎赉特旗| 尼木县| 山阳县| 乾安县| 习水县| 平遥县| 汕头市| 黎川县| 林周县| 石嘴山市| 乌拉特后旗| 玛纳斯县| 海丰县| 武安市| 太仆寺旗| 通化市| 湛江市| 景德镇市| 营山县| 海兴县| 富平县| 资阳市| 岳西县| 卓资县| 冀州市| 屯昌县| 竹溪县| 新宾| 宁德市| 陇南市| 安达市| 胶州市|