python使用BeautifulSoup分析網(wǎng)頁(yè)信息的方法
本文實(shí)例講述了python使用BeautifulSoup分析網(wǎng)頁(yè)信息的方法。分享給大家供大家參考。具體如下:
這段python代碼查找網(wǎng)頁(yè)上的所有鏈接,分析所有的span標(biāo)簽,并查找class包含titletext的span的內(nèi)容
import urllib2
#specify the url you want to query
url = "http://www.python.org"
#Query the website and return the html to the variable 'page'
page = urllib2.urlopen(url)
#import the Beautiful soup functions to parse the data returned from the website
from BeautifulSoup import BeautifulSoup
#Parse the html in the 'page' variable, and store it in Beautiful Soup format
soup = BeautifulSoup(page)
#to print the soup.head is the head tag and soup.head.title is the title tag
print soup.head
print soup.head.title
#to print the length of the page, use the len function
print len(page)
#create a new variable to store the data you want to find.
tags = soup.findAll('a')
#to print all the links
print tags
#to get all titles and print the contents of each title
titles = soup.findAll('span', attrs = { 'class' : 'titletext' })
for title in allTitles:
print title.contents
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Pytorch:dtype不一致問(wèn)題(expected dtype Double but&
這篇文章主要介紹了Pytorch:dtype不一致問(wèn)題(expected dtype Double but got dtype Float),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02Python rabbitMQ如何實(shí)現(xiàn)生產(chǎn)消費(fèi)者模式
這篇文章主要介紹了Python rabbitMQ如何實(shí)現(xiàn)生產(chǎn)消費(fèi)者模式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08基于Python實(shí)現(xiàn)文件分類器的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何基于Python實(shí)現(xiàn)文件分類器,目的主要是為了將辦公過(guò)程中產(chǎn)生的各種格式的文件完成整理,感興趣的可以了解一下2023-04-04關(guān)于python寫入文件自動(dòng)換行的問(wèn)題
今天小編就為大家分享一篇關(guān)于python寫入文件自動(dòng)換行的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06解決Jupyter notebook更換主題工具欄被隱藏及添加目錄生成插件問(wèn)題
這篇文章主要介紹了解決Jupyter notebook更換主題工具欄被隱藏及添加目錄生成插件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04python使用線程封裝的一個(gè)簡(jiǎn)單定時(shí)器類實(shí)例
這篇文章主要介紹了python使用線程封裝的一個(gè)簡(jiǎn)單定時(shí)器類,實(shí)例分析了Python線程的使用及定時(shí)器類的實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-05-05Python使用matplotlib的pie函數(shù)繪制餅狀圖功能示例
這篇文章主要介紹了Python使用matplotlib的pie函數(shù)繪制餅狀圖功能,結(jié)合實(shí)例形式分析了Python使用matplotlib的pie函數(shù)進(jìn)行餅狀圖繪制的具體操作技巧,注釋中對(duì)pie函數(shù)的用法進(jìn)行了詳細(xì)的說(shuō)明,便于理解,需要的朋友可以參考下2018-01-01