-
實現(xiàn)方式:
1. 內(nèi)存:python 內(nèi)存,待爬取url集合:set(),已爬取:set()。 set可以去掉重復(fù)的
2. 關(guān)系數(shù)據(jù)庫(表形式)urls(url,is_crawled)
3. 緩存數(shù)據(jù)庫 redis 待/已爬取集合:set,支持set (高性能,都用它)
查看全部 -
URL管理器:防止重復(fù)抓取,循環(huán)抓取
查看全部 -
看圖就好了
查看全部 -
web crawler調(diào)度端→URL管理器→網(wǎng)頁下載器→網(wǎng)頁解析器→價值數(shù)據(jù)
查看全部 -
抓取想要信息
查看全部 -
import re? #導(dǎo)入正則表達(dá)式要用的模塊
from bs4 import BeautifulSoup
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a class="sister" id="link1">Elsie</a>,
<a class="sister" id="link2">Lacie</a> and
<a class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
soup=BeautifulSoup(html_doc,'html.parser')? #(文檔字符串,解析器)
print('獲取所有鏈接:')
links=soup.find_all('a')
for link in links:
? ? print(link.name,link['href'],link.get_text())? #(名稱,URL,文字)
print('獲取指定鏈接(獲取Lacie鏈接):')
#link_node=soup.find('a',id="link2") 運(yùn)行結(jié)果一樣
link_node=soup.find('a',)? #注意find和find_all
print(link_node.name,link_node['href'],link_node.get_text())
print('輸入正則模糊匹配出需要的內(nèi)容:')
link_node=soup.find('a',href=re.compile(r"ill"))? #'r'表示正則中出現(xiàn)反斜線時,我們只需寫一個反斜線,否則我們要寫兩個
print(link_node.name,link_node['href'],link_node.get_text())
print('輸入p這個段落文字(指定class獲取內(nèi)容):')
p_node=soup.find('p',class_="story")
print(p_node.name,p_node.get_text())
輸出:
獲取所有鏈接: a?http://example.com/elsie?Elsie a?http://example.com/lacie?Lacie a?http://example.com/tillie?Tillie 獲取指定鏈接(獲取Lacie鏈接): a?http://example.com/lacie?Lacie 輸入正則模糊匹配出需要的內(nèi)容: a?http://example.com/tillie?Tillie 輸入p這個段落文字(指定class獲取內(nèi)容): p?Once?upon?a?time?there?were?three?little?sisters;?and?their?names?were Elsie, Lacie?and Tillie; and?they?lived?at?the?bottom?of?a?well.
查看全部 -
下載 網(wǎng)頁的方法 ?1 :最簡潔的方法 ?
import urllib 2 ?
response = urllib2.urlopen(‘網(wǎng)頁地址’)
print reponse. getcoed ? 獲取 狀態(tài)碼 如果 200 ?表示獲取成功
查看全部 -
新查看全部
-
基本構(gòu)成。查看全部
-
urllib2實戰(zhàn)演示查看全部
-
beautifulsoup安裝與測試查看全部
-
訪問節(jié)點(diǎn)信息
node,name
node['href']
node.get_text()
查看全部 -
搜索節(jié)點(diǎn)(find_all,find)
soup.find_all('a')
soup.find_all('a',href='/view/123.htm')
soup.find_all('a',href='re.compile(r'/view/\d+\.htm'))
soup.find_all('div',class_='abc',string='python')
查看全部 -
創(chuàng)建BeautifulSoup對象
from bs4 import? BeautifulSoup
#根據(jù)HTML網(wǎng)頁字符串創(chuàng)建BeautifulSoup對象
soup = BeautifulSoup(
????????????????????????????html_doc,
????????????????????????????'html.parser'
????????????????????????????from_encoding='utf8'
????????????????????????????)
查看全部 -
結(jié)構(gòu)化解析-DOM樹查看全部
舉報