python使用BeautifulSoup分析網(wǎng)頁信息的方法
本文實例講述了python使用BeautifulSoup分析網(wǎng)頁信息的方法。分享給大家供大家參考。具體如下:
這段python代碼查找網(wǎng)頁上的所有鏈接,分析所有的span標簽,并查找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
希望本文所述對大家的Python程序設計有所幫助。
相關文章
Pytorch:dtype不一致問題(expected dtype Double but&
這篇文章主要介紹了Pytorch:dtype不一致問題(expected dtype Double but got dtype Float),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02Python rabbitMQ如何實現(xiàn)生產(chǎn)消費者模式
這篇文章主要介紹了Python rabbitMQ如何實現(xiàn)生產(chǎn)消費者模式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-08-08解決Jupyter notebook更換主題工具欄被隱藏及添加目錄生成插件問題
這篇文章主要介紹了解決Jupyter notebook更換主題工具欄被隱藏及添加目錄生成插件問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04Python使用matplotlib的pie函數(shù)繪制餅狀圖功能示例
這篇文章主要介紹了Python使用matplotlib的pie函數(shù)繪制餅狀圖功能,結合實例形式分析了Python使用matplotlib的pie函數(shù)進行餅狀圖繪制的具體操作技巧,注釋中對pie函數(shù)的用法進行了詳細的說明,便于理解,需要的朋友可以參考下2018-01-01