国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

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

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

# -*- coding:utf-8 -*-#python 2.7#http://tieba.baidu.com/p/2460150866#標簽操作 from bs4 import BeautifulSoupimport urllib.requestimport 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 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標簽,只會查找出一個a標簽#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標簽的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 如何獲取頁面所有a標簽下href的值

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

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 波多野结衣一级视频 | aaa在线观看高清免费 | 性日韩精品 | 视频在线亚洲 | 日韩美女一级毛片 | 亚洲第一网色综合久久 | 精品国产一区二区三区不卡蜜臂 | 国产成人欧美一区二区三区的 | 国产精品亚洲视频 | 国产日韩高清一区二区三区 | 免费一区二区 | 日本亚洲欧美高清专区vr专区 | 美女被免费视频的网站 | 美国一级毛片片aaa 美国一级毛片片aa成人 | 国产亚洲欧美一区二区三区 | 国产欧美一区视频在线观看 | 亚洲一区二区三区91 | 欧美日韩亚洲一区二区三区 | 欧美高清一区二区三区欧美 | 久久综合中文字幕一区二区三区 | 亚洲综合国产一区二区三区 | 欧美黄网在线 | 欧美亚洲网站 | 美女张开腿黄网站免费国产 | 成年人黄色免费网站 | 国产精品久久久久久影院 | 一级一黄在线观看视频免费 | 亚洲欧美在线观看播放 | 国产天堂 | 欧美一区高清 | 亚洲国产精品二区久久 | 国产日韩欧美精品一区二区三区 | 色老头一区二区三区在线观看 | 欧美男女网站 | 国产成人啪精品 | 一区二区三区不卡在线观看 | 亚洲精品国产一区二区三 | 欧美另类69xxxxx极品 | 亚洲加勒比久久88色综合1 | 老司机午夜性生免费福利 | 日韩色网站 |