python3 BeautifulSoup模塊使用字典的方法抓取a標(biāo)簽內(nèi)的數(shù)據(jù)示例
更新時間:2019年11月28日 09:08:24 作者:weixin_34351321
這篇文章主要介紹了python3 BeautifulSoup模塊使用字典的方法抓取a標(biāo)簽內(nèi)的數(shù)據(jù),結(jié)合實例形式Fenix了python3 BeautifulSoup模塊進(jìn)行數(shù)據(jù)的抓取相關(guān)操作技巧,需要的朋友可以參考下
本文實例講述了python3 BeautifulSoup模塊使用字典的方法抓取a標(biāo)簽內(nèi)的數(shù)據(jù)。分享給大家供大家參考,具體如下:
# -*- coding:utf-8 -*-
#python 2.7
#XiaoDeng
#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)簽中含有其他標(biāo)簽,比如..,此時要提取中的數(shù)據(jù),需要用k.get_text()
#tag.get('calss'),也可以達(dá)到這個效果
Python Socket編程技巧總結(jié)》、《Python正則表達(dá)式用法總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
python實現(xiàn)發(fā)送和獲取手機短信驗證碼
這篇文章主要介紹了python實現(xiàn)發(fā)送和獲取手機短信驗證碼的相關(guān)資料,講解了python如何解決接口測試獲取手機驗證碼問題,感興趣的小伙伴們可以參考一下2016-01-01
Python利用Charles 實現(xiàn)全部自動答題思路流程分析
這篇文章主要介紹了Python利用Charles 實現(xiàn)全部自動答題思路流程分析,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08
Python網(wǎng)頁正文轉(zhuǎn)換語音文件的操作方法
這篇文章主要介紹了Python網(wǎng)頁正文轉(zhuǎn)換語音文件的操作方法,需要的朋友可以參考下2018-12-12
Python實現(xiàn)SMTP發(fā)送郵件詳細(xì)教程
這篇文章主要為大家詳細(xì)介紹了Python實現(xiàn)SMTP發(fā)送郵件詳細(xì)教程,附SMTP郵件發(fā)送代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08
Python爬蟲框架scrapy實現(xiàn)的文件下載功能示例
這篇文章主要介紹了Python爬蟲框架scrapy實現(xiàn)的文件下載功能,結(jié)合實例形式分析了scrapy框架進(jìn)行文件下載的具體操作步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2018-08-08

