python 如何獲取頁(yè)面所有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)址,可以用這個(gè)辦法來(lái)讀取網(wǎng)頁(yè) #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') #文檔對(duì)象 #查找a標(biāo)簽,只會(huì)查找出一個(gè)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>,此時(shí)要提取<a>中的數(shù)據(jù),需要用k.get_text()
soup = BeautifulSoup(html, 'html.parser') #文檔對(duì)象 #查找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爬蟲(chóng)獲取任意頁(yè)面的標(biāo)簽和屬性(包括獲取a標(biāo)簽的href屬性)
看代碼吧~
# coding=utf-8 from bs4 import BeautifulSoup import requests # 定義一個(gè)獲取url頁(yè)面下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)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
- python爬蟲(chóng)之異常捕獲及標(biāo)簽過(guò)濾詳解
- Python深度學(xué)習(xí)之圖像標(biāo)簽標(biāo)注軟件labelme詳解
- python中Tkinter實(shí)現(xiàn)分頁(yè)標(biāo)簽的示例代碼
- Python 實(shí)現(xiàn)自動(dòng)完成A4標(biāo)簽排版打印功能
- Python氣泡提示與標(biāo)簽的實(shí)現(xiàn)
- Python 生成VOC格式的標(biāo)簽實(shí)例
- 基于python3生成標(biāo)簽云代碼解析
- python 實(shí)現(xiàn)添加標(biāo)簽&打標(biāo)簽的操作
相關(guān)文章
Python實(shí)例方法、類(lèi)方法、靜態(tài)方法的區(qū)別與作用詳解
這篇文章主要介紹了Python實(shí)例方法、類(lèi)方法、靜態(tài)方法的區(qū)別與作用,結(jié)合實(shí)例形式分析了Python面向?qū)ο蟪绦蛟O(shè)計(jì)中實(shí)例方法、類(lèi)方法、靜態(tài)方法的概念、原理、用法及相關(guān)操作技巧,需要的朋友可以參考下2019-03-03在Python的while循環(huán)中使用else以及循環(huán)嵌套的用法
這篇文章主要介紹了在Python的while循環(huán)中使用else以及循環(huán)嵌套的用法,是Python入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-10-10Python切換pip源兩種方法(解決pip?install慢)
這篇文章主要給大家介紹了關(guān)于Python切換pip源兩種方法(解決pip?install慢),我總結(jié)的這幾種更換pip源的常用方式,希望可以幫助您成功配置國(guó)內(nèi)源,解決安裝Python包速度慢的問(wèn)題,需要的朋友可以參考下2023-11-11Python requests及aiohttp速度對(duì)比代碼實(shí)例
這篇文章主要介紹了Python requests及aiohttp速度對(duì)比代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07輕松掌握Python爬蟲(chóng),從入門(mén)到精通
Python爬蟲(chóng)學(xué)習(xí)完整版來(lái)了!想成為一名爬蟲(chóng)高手,掌握數(shù)據(jù)采集的技能嗎?這份指南將帶你從零開(kāi)始,一步步掌握Python爬蟲(chóng)的各種技巧,讓你輕松獲取海量數(shù)據(jù),需要的朋友可以參考下2024-03-03Python機(jī)器學(xué)習(xí)之KNN近鄰算法
KNN可以說(shuō)是最簡(jiǎn)單的分類(lèi)算法之一,同時(shí),它也是最常用的分類(lèi)算法,文中非常詳細(xì)的介紹了該算法,對(duì)正在學(xué)習(xí)python的小伙伴們有很好的幫助,需要的朋友可以參考下2021-05-05Python count()函數(shù)實(shí)例詳解
count() 是Python的內(nèi)置函數(shù),可以「統(tǒng)計(jì)」字符串里指定「字符」或指定字符串出現(xiàn)的「次數(shù)」,這篇文章主要介紹了Python count()函數(shù)詳解,需要的朋友可以參考下2023-07-07解決pip install的時(shí)候報(bào)錯(cuò)timed out的問(wèn)題
今天小編就為大家分享一篇解決pip install的時(shí)候報(bào)錯(cuò)timed out的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06