av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

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

瀏覽:33日期:2022-06-20 14:31:38
看代碼吧~

# -*- coding:utf-8 -*-#python 2.7#http://tieba.baidu.com/p/2460150866#標(biāo)簽操作 from bs4 import BeautifulSoupimport urllib.requestimport 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 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' id='xiaodeng'><!-- Elsie --></a>,<a rel='external nofollow' rel='external nofollow' id='link2'>Lacie</a> and<a rel='external nofollow' id='link3'>Tillie</a>;<a rel='external nofollow' rel='external nofollow' 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)簽,只會(huì)查找出一個(gè)a標(biāo)簽#print(soup.a)#<a 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’) #文檔對象#查找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)

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

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

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 国产在线观看一区 | 国产在线网址 | 91视频免费在观看 | 日韩欧美精品在线 | 久久亚洲一区二区三区四区 | 中文字幕视频三区 | 国产精品福利在线观看 | 成人久久久 | 久久国产欧美日韩精品 | 亚洲色图第一页 | 亚洲福利在线视频 | 免费人成在线观看网站 | 求毛片| 久久av一区二区三区 | 亚洲成人一级 | 一区二区视频 | 中文字幕乱码视频32 | 免费成人高清在线视频 | 欧美天堂 | 欧美精品一区在线发布 | 国内精品久久久久 | 亚洲www啪成人一区二区麻豆 | 韩国电影久久 | www国产成人免费观看视频,深夜成人网 | 91成人在线 | 免费观看一级特黄欧美大片 | 久久高清 | 成人精品福利 | 99久久日韩精品免费热麻豆美女 | 精品国产成人 | 欧美精品成人一区二区三区四区 | 一级a性色生活片久久毛片 一级特黄a大片 | 欧美精品久久 | 黑人一级片视频 | japan25hdxxxx日本 做a的各种视频 | 蜜桃av一区二区三区 | 男人午夜视频 | 亚洲va国产日韩欧美精品色婷婷 | 一级片免费视频 | 超碰在线播 | 成人在线小视频 |