色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

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

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

瀏覽:5日期: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 編程
相關文章:
主站蜘蛛池模板: 九九黄色影院 | 国产日韩精品视频 | 国产精品免费观看视频 | 国产午夜精品理论片久久影视 | 在线有码| 久久在线免费观看视频 | 最新中文字幕视频 | 国产高清精品久久久久久久 | 免费人成年短视频在线观看网站 | 亚洲第十色 | 国产成人a毛片在线 | 久久久久网站 | 依依成人综合网 | 免费在线观看一区 | 美女扒开双腿让男人爽透视频 | 九九视频在线观看视频6偷拍 | 欧美亚洲一区二区三区 | 九月婷婷亚洲综合在线 | 一级片一区| 日本大臿亚洲香蕉大片 | 99色视频在线观看 | 久久精品a亚洲国产v高清不卡 | 最新国产三级在线不卡视频 | 青青视频国产依人在线 | 久色网址 | 午夜精品在线 | 欧美18在线 | 国产中的精品一区的 | 久久网免费 | 亚洲国产成人久久精品图片 | 伊人午夜| 久久999视频 | 波野多结衣在线观看 | 国产视频一区二区三区四区 | a级毛片免费观看视频 | 国产精品1页 | 欧美刺激午夜性久久久久久久 | 亚洲欧美日本综合一区二区三区 | 中国内地毛片免费高清 | 欧美亚洲国产视频 | 男女上下爽无遮挡午夜免费视频 |