對網(wǎng)頁信息的抓取。
一.常用的用是re,BeautifulSoup以及l(fā)xml。其中re,lxml速度快。
re比較復(fù)雜,當(dāng)頁面發(fā)生變化時正則表達(dá)式還需修改。
BeautifulSoup比較簡單,但是速度慢。
lxml+cssselect速度快,也比較簡單,可以說是集合了re和BeautifulSoup的優(yōu)點(diǎn)。
二.用lxml來抓取網(wǎng)站信息
1.以一個網(wǎng)站為例:
抓取面積信息
url='http://example.webscraping.com/places/view/United-Kingdom-239'
還是用之前寫的download函數(shù)
from urllib.request import *
from urllib.parse import *
from urllib.error import URLError
import lxml.html
def download(url,User_agent='wswp',proxy=None,num_retry=2):
print('Downloading:',url)
headers={'User-agent':User_agent}
request=Request(url,headers=headers)
#加入代理服務(wù)器的處理,就不用urlopen來下載網(wǎng)頁了,而是用自己構(gòu)建的opener來打開
opener=build_opener()
#若設(shè)置了代理,執(zhí)行下面操作加入代理到opener中
if proxy:
proxy_params={urlparse(url).scheme:proxy}
opener.add_handler(ProxyHandler(proxy_params))#在自己構(gòu)建的瀏覽器中加入了代理服務(wù)器
#當(dāng)沒有設(shè)置代理時,下面的打開方式和urlopen是一樣的
try:
html=opener.open(request).read()
except URLError as e:#引入URLError進(jìn)行分析
html=None
print('Download error:',e.reason)
if num_retry>0:
if hasattr(e,'code') and 500<=e.code<600:
return download(url,num_retry=num_retry-1)
return html
html=download(url).decode()#lxml的輸入要是字符串形式
tree=lxml.html.fromstring(html)
td=tree.cssselect('tr#places_area__row>td.w2p_fw')[0]#cssselect選擇出來的是一個列表,所以要取出里面的元素
ares=td.text_content()
print(ares)
2.要抓取全部的信息
fields={'area','population','iso','country','capital','continent','tld','currency_code','currency_name','phone','postal_code_format','postal_code_regex','languages','neighbours'}
def lxml_scraper(html):
tree=lxml.html.fromstring(html)
results={}
for field in fields:
td=tree.cssselect('tr#places_%s__row>td.w2p_fw'%field)[0]
result=td.text_content()
results[field]=result
return results
3.加入爬蟲中:回調(diào)函數(shù)
def scrape_callback(url,html):
if re.search('/view/',url):
tree=lxml.html.fromstring(html)
row=[tree.cssselect('table>tr#places_%s__row>td.w2p_fw'%field)[0].text_content() for field in fields]
print(url,row)
4.在回調(diào)中寫人csv文件:
import csv
class ScrapeCallback:
def __init__(self):
self.fields=['area','population','iso','country','capital','continent','tld','currency_code','currency_name','phone','postal_code_format','postal_code_regex','languages','neighbours']
self.writer=csv.writer(open(r'C:\Users\Desktop\python學(xué)習(xí)筆記\table1.csv','w',newline=''))
self.writer.writerow(self.fields)
def __call__(self,url,html):
if re.search('/view/',url):
row=[]
for field in self.fields:
tree=lxml.html.fromstring(html)
row.append(tree.cssselect('table>tr#places_%s__row>td.w2p_fw'%field)[0].text_content())
self.writer.writerow(row)
link_crawler('http://example.webscraping.com','/(index|view)',delay=1,maxdepth=-1)
最終版本:
import time
import re
from urllib import parse
from urllib import robotparser
import csv
from urllib.request import *
from urllib.parse import *
from urllib.error import URLError
import lxml.html
class Timedelay:
#初始化
def __init__(self,delay):
#設(shè)置延遲時間
self.delay=delay
#創(chuàng)建記錄主站的字典
self.domains={}
#創(chuàng)建等待函數(shù),同時還要實現(xiàn)記錄走后一次訪問時間
def wait(self,url):
netloc=urlparse(url).netloc
last_time=self.domains.get(netloc)
if self.delay and last_time:
sleeptime=self.delay-(time.time()-last_time)
if sleeptime>0:
time.sleep(sleeptime)
#每次暫停后,或者沒暫停都重置最后一次訪問時間
self.domains[netloc]=time.time()
class ScrapeCallback:
def __init__(self):
self.fields=['area','population','iso','country','capital','continent','tld','currency_code','currency_name','phone','postal_code_format','postal_code_regex','languages','neighbours']
self.writer=csv.writer(open(r'C:\Users\Desktop\python學(xué)習(xí)筆記\table1.csv','w',newline=''))
self.writer.writerow(self.fields)
def __call__(self,url,html):
if re.search('/view/',url):
row=[]
for field in self.fields:
tree=lxml.html.fromstring(html)
row.append(tree.cssselect('table>tr#places_%s__row>td.w2p_fw'%field)[0].text_content())
self.writer.writerow(row)
def download(url,User_agent='wswp',proxy=None,num_retry=2):
print('Downloading:',url)
headers={'User-agent':User_agent}
request=Request(url,headers=headers)
#加入代理服務(wù)器的處理,就不用urlopen來下載網(wǎng)頁了,而是用自己構(gòu)建的opener來打開
opener=build_opener()
#若設(shè)置了代理,執(zhí)行下面操作加入代理到opener中
print('start proxy',proxy)
if proxy:
print('proxy is:',proxy)
proxy_params={urlparse(url).scheme:proxy}
opener.add_handler(ProxyHandler(proxy_params))#在自己構(gòu)建的瀏覽器中加入了代理服務(wù)器
#當(dāng)沒有設(shè)置代理時,下面的打開方式和urlopen是一樣的
try:
html=opener.open(request).read()
except URLError as e:#引入URLError進(jìn)行分析
html=None
print('Download error:',e.reason)
if num_retry>0:
if hasattr(e,'code') and 500<=e.code<600:
return download(url,num_retry=num_retry-1)
return html
def get_link(html):
webpage_patt=re.compile('<a[^>]+href=["\'](.*?)["\']',re.IGNORECASE)
return webpage_patt.findall(html.decode())#返回一個包含所以頁面link的列表
def link_crawler(seed_url,link_res,User_agent='wswp',delay=None,proxy=None,maxdepth=2,scrape_callback=ScrapeCallback()):
crawl_queue=[seed_url]
#seen=set(crawl_queue)
seen={}
seen[seed_url]=0
#讀取robots.txt
rp=robotparser.RobotFileParser()
rp.set_url('http://example.webscraping.com/robots.txt')
rp.read()
timedelay=Timedelay(delay)#同樣是初始化
'''
這里的前幾行是初始賦值的作用,后面的循環(huán)中
就不再需要賦值了,特別是在循環(huán)中很難操作set()
使其增加
'''
while crawl_queue:
url=crawl_queue.pop()
#檢查該url是否能被禁止爬取
if rp.can_fetch(User_agent,url):
timedelay.wait(url)#暫停,并記下本次主站下載開始時間
html=download(url,proxy=proxy)
dept=seen[url]#獲取現(xiàn)在的深度
links=[]
links.extend(scrape_callback(url,html.decode()) or [])
#加入一個過濾器#在過濾器中看是否重復(fù)
if dept!=maxdepth:
for link in get_link(html):
if re.match(link_res,link):
link=parse.urljoin(seed_url,link)
if link not in seen:#先篩選符合條件的link,再進(jìn)行篩選是否看過,這個順序能減少工作量。
crawl_queue.append(link)
seen[link]=dept+1#新加入的網(wǎng)址都要在原來的深度上加一
else:
print('Blocked by robots.txt',url)
print(seen,links)
def get_link(html):
webpage_patt=re.compile('<a[^>]+href=["\'](.*?)["\']',re.IGNORECASE)
return webpage_patt.findall(html.decode())#返回一個包含所以頁面link的列表
link_crawler('http://example.webscraping.com','/(index|view)',delay=1,maxdepth=-1)