1. 程式人生 > >python網路爬蟲與資訊提取(四)Robots協議

python網路爬蟲與資訊提取(四)Robots協議

Robots協議  例項一京東 例項二亞馬遜

緒論 網路爬蟲引發的問題

1、網路爬蟲的尺寸

爬取網頁 Requests庫

爬取網站 Scrapy庫

爬取全網 建立搜尋引擎

2、網路爬蟲引發的問題

1.伺服器效能騷擾

2.法律風險

3.洩露隱私

3、網路爬蟲的限制

來源審查:判斷User-Agent進行限制

      檢查來訪HTTP協議頭的User-Agent域,只響應瀏覽器或友好爬蟲的訪問

釋出公告:

      告知所有爬蟲網站的爬取策略,要求爬蟲遵守。


一、Robots協議

Robots Exclusion Standard 網路爬蟲排除標準

作用:網站告知網路爬蟲哪些頁面可以抓取,哪些不行。

形式:在網站根目錄下的robots。txt檔案

User-agent: *               #不可以爬取/?開頭的網頁  等等……
Disallow: /?* 
Disallow: /pop/*.html 
Disallow: /pinpai/*.html?* 
User-agent: EtaoSpider      #以下四種spider為惡意爬蟲,不可以爬任何網頁
Disallow: / 
User-agent: HuihuiSpider 
Disallow: / 
User-agent: GwdangSpider 
Disallow: / 
User-agent: WochachaSpider 
Disallow: /

注:Robots協議基本語法      

# 註釋  *代表所有  /代表根目錄

二、Robots協議的遵守方式

1.Robots協議的使用

網路爬蟲:自動或人工識別robots.txt,再進行內容爬取。

約束性:Robots協議是建議但非約束性,可不遵守,但存在法律風險。

2.對Robots協議的理解

結合網路爬蟲尺寸,尺寸越大,越要遵守,防範法律風險。

類人行為可不遵守,閱讀頻率小,一次瀏覽內容少,注意不可用於商業用途。

>>> r = requests.get("https://item.jd.com/6946605.html")
>>> r.status_code
200
>>> r.encoding
'gbk'
>>> r.text[:1000]   #取網頁前1000個字元
'<!DOCTYPE HTML>\n<html lang="zh-CN">\n<head>\n    <!-- shouji -->\n    <meta http-equiv="Content-Type" content="text/html; charset=gbk" />\n    <title>【華為P20】華為 HUAWEI P20  AI智慧全面屏 6GB +64GB 亮黑色 全網通版 移動聯通電信4G手機 雙卡雙待【行情 報價 價格 評測】-京東</title>\n    <meta name="keywords" content="HUAWEIP20,華為P20,華為P20報價,HUAWEIP20報價"/>\n    <meta name="description" content="【華為P20】京東JD.COM提供華為P20正品行貨,幷包括HUAWEIP20網購指南,以及華為P20圖片、P20引數、P20評論、P20心得、P20技巧等資訊,網購華為P20上京東,放心又輕鬆" />\n    <meta name="format-detection" content="telephone=no">\n    <meta http-equiv="mobile-agent" content="format=xhtml; url=//item.m.jd.com/product/6946605.html">\n    <meta http-equiv="mobile-agent" content="format=html5; url=//item.m.jd.com/product/6946605.html">\n    <meta http-equiv="X-UA-Compatible" content="IE=Edge">\n    <link rel="canonical" href="//item.jd.com/6946605.html"/>\n        <link rel="dns-prefetch" href="//misc.360buyimg.com"/>\n    <link rel="dns-prefetch" href="//static.360buyimg.com"/>\n    <link rel="dns-prefetch" href="//img10.36'

例項一:爬取京東商品網頁

>>> import requests
>>> url = "https://item.jd.com/6946605.html"
>>> try:
    r = requests.get(url)
    r.raise_for_status()           #此函式返回200不產生異常,否則產生異常
    r.encoding = r.apparent_encoding
    print(r.text[:1000])
except:
    print("爬取失敗")
#上為網路爬蟲程式碼框架
    
<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <!-- shouji -->
    <meta http-equiv="Content-Type" content="text/html; charset=gbk" />
    <title>【華為P20】華為 HUAWEI P20  AI智慧全面屏 6GB +64GB 亮黑色 全網通版 移動聯通電信4G手機 雙卡雙待【行情 報價 價格 評測】-京東</title>
    <meta name="keywords" content="HUAWEIP20,華為P20,華為P20報價,HUAWEIP20報價"/>
    <meta name="description" content="【華為P20】京東JD.COM提供華為P20正品行貨,幷包括HUAWEIP20網購指南,以及華為P20圖片、P20引數、P20評論、P20心得、P20技巧等資訊,網購華為P20上京東,放心又輕鬆" />
    <meta name="format-detection" content="telephone=no">
    <meta http-equiv="mobile-agent" content="format=xhtml; url=//item.m.jd.com/product/6946605.html">

例項二:亞馬遜商品頁

>>> r = requests.get("www.amazon.cn/gp/product/B01M8L5Z3Y")
>>> r.status_code
503
>>>r.encoding
'ISO-8859-1'
>>>r.encoding = r.apparent_encoding
>>>print(r.text)
#出現錯誤 由於API造成  檢查head
>>>r.requests.headers
{'User-Agent': 'python-requests/2.18.4', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
#網站發現了python爬蟲的request請求,並拒絕了,利用headers控制引數修改user-agent
>>> kv = {'user-agent':'Mozilla/5.0'}											      
>>> url = "https://www.amazon.cn/gp/product/B01M8L5Z3Y"										
>>> r = requests.get(url,headers = kv)											      
>>> r.status_code											      
200
>>> r.request.headers											      
{'user-agent': 'Mozilla/5.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
>>> r.text[:1000]
											      
'\n\n\n\n\n\n\n\n  \n  \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n    \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n    \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n    \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n    <!doctype html><html class="a-no-js" data-19ax5a9jf="dingo">\n    <head>\n<script type="text/javascript">var ue_t0=ue_t0||+new Date();</script>\n<script type="text/javascript">\n\nvar ue_hob=+new Date();\nvar ue_id=\'DN1BM72NDXD7JV3A36YV\',\nue_csm = window,\nue_err_chan = \'jserr-rw\',\nue = {};\n(function(d){var e=d.ue=d.ue||{},f=Date.now||function(){return+new Date};e.d=function(b){return f()-(b?0:d.ue_t0)};e.stub=function(b,a){if(!b[a]){var c=[];b[a]=function(){c.push([c.slice.call(arguments),e.d(),d.ue_id])};b[a].replay=function(b){for(var a;a=c.shift();)b(a[0],a[1],a[2])};b[a].isStub=1}};e.exec=function(b,a){return function(){if(1==window.ueinit)try{return b.apply(this,arguments)}catch(c){ueLogError(c,{attribution:a||"undefined",logLevel:"WARN"})}}}})(ue_csm);\n\nue.stub(ue,"log");ue.stub(ue,"onunload");ue.stub(ue,"onflush");\n\n(function(d,e){function h(f,b){if(!(a.ec>a.mxe)&&f){a.ter.'

         爬蟲程式碼框架

>>> import requests
>>> url = "https://item.jd.com/6946605.html"
>>> try:
    kv = {'user-agent':'Mozilla/5.0'}
    r = requests.get(url,headers = kv)
    r.raise_for_status() 
    r.encoding = r.apparent_encoding
    print(r.text[:1000])
except:
    print("爬取失敗")

例項三:百度360搜尋關鍵詞