最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

看代碼吧~

# -*- 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)址,可以用這個辦法來讀取網(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')   #文檔對象 
 
#查找a標(biāo)簽,只會查找出一個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>,此時要提取<a>中的數(shù)據(jù),需要用k.get_text()

soup = BeautifulSoup(html, 'html.parser')   #文檔對象
#查找a標(biāo)簽,只會查找出一個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>,此時要提取<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 
# 定義一個獲取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)

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

相關(guān)文章

最新評論

石首市| 卢龙县| 温州市| 黄陵县| 商水县| 玛多县| 云和县| 南阳市| 上犹县| 专栏| 山西省| 集贤县| 永吉县| 辉南县| 临沂市| 肥西县| 巴楚县| 毕节市| 江川县| 彩票| 凤山市| 永仁县| 罗定市| 汝阳县| 儋州市| 蕲春县| 陕西省| 玉山县| 旅游| 当阳市| 皋兰县| 孟津县| 浪卡子县| 达州市| 镇江市| 西畴县| 丰县| 荣昌县| 谷城县| 隆昌县| 曲周县|