這篇文章主要講解了“怎么用Web Scraping爬取HTML網(wǎng)頁”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么用Web Scraping爬取HTML網(wǎng)頁”吧!
十多年的茂南網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。網(wǎng)絡(luò)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整茂南建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)從事“茂南網(wǎng)站設(shè)計(jì)”,“茂南網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
-爬取HTML網(wǎng)頁
-直接下載數(shù)據(jù)文件,例如csv,txt,pdf文件
-通過應(yīng)用程序編程接口(API)訪問數(shù)據(jù),例如 電影數(shù)據(jù)庫,Twitter
選擇網(wǎng)頁爬取,當(dāng)然了解HTML網(wǎng)頁的基本結(jié)構(gòu),可以參考這個(gè)網(wǎng)頁:
HTML的基本結(jié)構(gòu)
HTML標(biāo)記:head,body,p,a,form,table等等
標(biāo)簽會(huì)具有屬性。例如,標(biāo)記a具有屬性(或?qū)傩裕﹉ref的鏈接的目標(biāo)。
class和id是html用來通過級(jí)聯(lián)樣式表(CSS)控制每個(gè)元素的樣式的特殊屬性。 id是元素的唯一標(biāo)識(shí)符,而class用于將元素分組以進(jìn)行樣式設(shè)置。
一個(gè)元素可以與多個(gè)類相關(guān)聯(lián)。 這些類別之間用空格隔開,例如 <h3 class=“ city main”>倫敦</ h3>
下圖是來自W3SCHOOL的例子,city的包括三個(gè)屬性,main包括一個(gè)屬性,London運(yùn)用了兩個(gè)city和main,這兩個(gè)類,呈現(xiàn)出來的是下圖的樣子。
可以通過標(biāo)簽相對(duì)于彼此的位置來引用標(biāo)簽
child-child是另一個(gè)標(biāo)簽內(nèi)的標(biāo)簽,例如 這兩個(gè)p標(biāo)簽是div標(biāo)簽的子標(biāo)簽。
parent-parent是一個(gè)標(biāo)簽,另一個(gè)標(biāo)簽在其中,例如 html標(biāo)簽是body標(biāo)簽的parent標(biāo)簽。
siblings-siblings是與另一個(gè)標(biāo)簽具有相同parent標(biāo)簽的標(biāo)簽,例如 在html示例中,head和body標(biāo)簽是同級(jí)標(biāo)簽,因?yàn)樗鼈兌荚趆tml內(nèi)。 兩個(gè)p標(biāo)簽都是sibling,因?yàn)樗鼈兌荚赽ody里面。
四步爬取網(wǎng)頁:
第一步:安裝模塊
安裝requests,beautifulsoup4,用來爬取網(wǎng)頁信息
Install modules requests, BeautifulSoup4/scrapy/selenium/....requests: allow you to send HTTP/1.1 requests using Python. To install:Open terminal (Mac) or Anaconda Command Prompt (Windows)code: BeautifulSoup: web page parsing library, to install, use:
第二步 :利用安裝包來讀取網(wǎng)頁源碼
第三步:瀏覽網(wǎng)頁源碼找到需要讀取信息的位置
這里不同的瀏覽器讀取源碼有差異,下面介紹幾個(gè),有相關(guān)網(wǎng)頁查詢?cè)敿?xì)信息。
Firefox: right click on the web page and select "view page source"Safari: please instruction here to see page source ()Ineternet Explorer: see instruction at
第四步:開始讀取
Beautifulsoup: 簡單那,支持CSS Selector, 但不支持 XPathscrapy (): 支持 CSS Selector 和XPathSelenium: 可以爬取動(dòng)態(tài)網(wǎng)頁 (例如下拉不斷更新的)lxml等BeautifulSoup里Tag: an xml or HTML tag 標(biāo)簽Name: every tag has a name 每個(gè)標(biāo)簽的名字Attributes: a tag may have any number of attributes. 每個(gè)標(biāo)簽有一個(gè)到多個(gè)屬性 A tag is shown as a dictionary in the form of {attribute1_name:attribute1_value, attribute2_name:attribute2_value, ...}. If an attribute has multiple values, the value is stored as a listNavigableString: the text within a tag
上代碼:
#Import requests and beautifulsoup packages
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity="all"
# import requests package
import requests
# import BeautifulSoup from package bs4 (i.e. beautifulsoup4)
from bs4 import BeautifulSoup
Get web page content
# send a get request to the web page
page=requests.get("A simple example page")
# status_code 200 indicates success.
# a status code >200 indicates a failure
if page.status_code==200:
# content property gives the content returned in bytes
print(page.content) # text in bytes
print(page.text) # text in unicode
#Parse web page content
# Process the returned content using beautifulsoup module
# initiate a beautifulsoup object using the html source and Python’s html.parser
soup=BeautifulSoup(page.content, 'html.parser')
# soup object stands for the **root**
# node of the html document tree
print("Soup object:")
# print soup object nicely
print(soup.prettify())
# soup.children returns an iterator of all children nodes
print("\soup children nodes:")
soup_children=soup.children
print(soup_children)
# convert to list
soup_children=list(soup.children)
print("\nlist of children of root:")
print(len(soup_children))
# html is the only child of the root node
html=soup_children[0]
html
# Get head and body tag
html_children=list(html.children)
print("how many children under html? ", len(html_children))
for idx, child in enumerate(html_children):
print("Child {} is: {}\n".format(idx, child))
# head is the second child of html
head=html_children[1]
# extract all text inside head
print("\nhead text:")
print(head.get_text())
# body is the fourth child of html
body=html_children[3]
# Get details of a tag
# get the first p tag in the div of body
div=list(body.children)[1]
p=list(div.children)[1]
p
# get the details of p tag
# first, get the data type of p
print("\ndata type:")
print(type(p))
# get tag name (property of p object)
print ("\ntag name: ")
print(p.name)
# a tag object with attributes has a dictionary
# use <tag>.attrs to get the dictionary
# each attribute name of the tag is a key
# get all attributes
p.attrs
# get "class" attribute
print ("\ntag class: ")
print(p["class"])
# how to determine if 'id' is an attribute of p?
# get text of p tag
p.get_text()
感謝各位的閱讀,以上就是“怎么用Web Scraping爬取HTML網(wǎng)頁”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)怎么用Web Scraping爬取HTML網(wǎng)頁這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
當(dāng)前標(biāo)題:怎么用WebScraping爬取HTML網(wǎng)頁
網(wǎng)站路徑:http://www.rwnh.cn/article24/ghdije.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、網(wǎng)站內(nèi)鏈、云服務(wù)器、域名注冊(cè)、虛擬主機(jī)、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)