婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > python 如何獲取頁面所有a標簽下href的值

python 如何獲取頁面所有a標簽下href的值

熱門標簽:幫人做地圖標注收費算詐騙嗎 外呼不封號系統 蘇州電銷機器人十大排行榜 遼寧400電話辦理多少錢 溫州旅游地圖標注 悟空智電銷機器人6 荊州云電銷機器人供應商 江蘇房產電銷機器人廠家 電信營業廳400電話申請

看代碼吧~

# -*- coding:utf-8 -*-
#python 2.7
#http://tieba.baidu.com/p/2460150866
#標簽操作 
 
from bs4 import BeautifulSoup
import urllib.request
import re 
 
#如果是網址,可以用這個辦法來讀取網頁
#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')   #文檔對象 
 
#查找a標簽,只會查找出一個a標簽
#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標簽的class屬性
    print(k['id'])#查a標簽的id值
    print(k['href'])#查a標簽的href值
    print(k.string)#查a標簽的string 
    

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

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

如果,標簽a>中含有其他標簽,比如em>../em>,此時要提取a>中的數據,需要用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)

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

看代碼吧~

# coding=utf-8 
from bs4 import BeautifulSoup 
import requests 
# 定義一個獲取url頁面下label標簽的attr屬性的函數 
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)

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

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

標簽:黃山 濟南 景德鎮 三沙 臺灣 喀什 欽州 宿遷

巨人網絡通訊聲明:本文標題《python 如何獲取頁面所有a標簽下href的值》,本文關鍵詞  python,如何,獲取,頁面,所有,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《python 如何獲取頁面所有a標簽下href的值》相關的同類信息!
  • 本頁收集關于python 如何獲取頁面所有a標簽下href的值的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 乐清市| 瑞昌市| 东兴市| 南汇区| 红原县| 监利县| 永登县| 阳谷县| 金坛市| 镇原县| 濮阳县| 焉耆| 沾益县| 大名县| 蓬安县| 和硕县| 西华县| 凌海市| 太白县| 班玛县| 巴里| 类乌齐县| 抚松县| 江永县| 上饶县| 德清县| 黄骅市| 廊坊市| 博爱县| 普格县| 乐都县| 景德镇市| 贞丰县| 烟台市| 密云县| 乌拉特前旗| 灵寿县| 新建县| 普格县| 朝阳县| 额济纳旗|