亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2021年05月06日 11:47:02   作者:不愿透露姓名的菜鳥  
這篇文章主要介紹了python 獲取頁面所有a標簽下href的值操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

看代碼吧~

# -*- 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>中的數(shù)據,需要用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>中的數(shù)據,需要用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屬性的函數(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)

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

相關文章

  • Python實例方法、類方法、靜態(tài)方法的區(qū)別與作用詳解

    Python實例方法、類方法、靜態(tài)方法的區(qū)別與作用詳解

    這篇文章主要介紹了Python實例方法、類方法、靜態(tài)方法的區(qū)別與作用,結合實例形式分析了Python面向對象程序設計中實例方法、類方法、靜態(tài)方法的概念、原理、用法及相關操作技巧,需要的朋友可以參考下
    2019-03-03
  • 在Python的while循環(huán)中使用else以及循環(huán)嵌套的用法

    在Python的while循環(huán)中使用else以及循環(huán)嵌套的用法

    這篇文章主要介紹了在Python的while循環(huán)中使用else以及循環(huán)嵌套的用法,是Python入門學習中的基礎知識,需要的朋友可以參考下
    2015-10-10
  • Python切換pip源兩種方法(解決pip?install慢)

    Python切換pip源兩種方法(解決pip?install慢)

    這篇文章主要給大家介紹了關于Python切換pip源兩種方法(解決pip?install慢),我總結的這幾種更換pip源的常用方式,希望可以幫助您成功配置國內源,解決安裝Python包速度慢的問題,需要的朋友可以參考下
    2023-11-11
  • Django REST framework視圖的用法

    Django REST framework視圖的用法

    這篇文章主要介紹了Django REST framework 視圖,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • Python requests及aiohttp速度對比代碼實例

    Python requests及aiohttp速度對比代碼實例

    這篇文章主要介紹了Python requests及aiohttp速度對比代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • 輕松掌握Python爬蟲,從入門到精通

    輕松掌握Python爬蟲,從入門到精通

    Python爬蟲學習完整版來了!想成為一名爬蟲高手,掌握數(shù)據采集的技能嗎?這份指南將帶你從零開始,一步步掌握Python爬蟲的各種技巧,讓你輕松獲取海量數(shù)據,需要的朋友可以參考下
    2024-03-03
  • PyQt5 如何讓界面和邏輯分離的方法

    PyQt5 如何讓界面和邏輯分離的方法

    這篇文章主要介紹了PyQt5 如何讓界面和邏輯分離的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-03-03
  • Python機器學習之KNN近鄰算法

    Python機器學習之KNN近鄰算法

    KNN可以說是最簡單的分類算法之一,同時,它也是最常用的分類算法,文中非常詳細的介紹了該算法,對正在學習python的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-05-05
  • Python count()函數(shù)實例詳解

    Python count()函數(shù)實例詳解

    count() 是Python的內置函數(shù),可以「統(tǒng)計」字符串里指定「字符」或指定字符串出現(xiàn)的「次數(shù)」,這篇文章主要介紹了Python count()函數(shù)詳解,需要的朋友可以參考下
    2023-07-07
  • 解決pip install的時候報錯timed out的問題

    解決pip install的時候報錯timed out的問題

    今天小編就為大家分享一篇解決pip install的時候報錯timed out的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06

最新評論