爬取網站前1_解析網站robots.txt檔案

Pop_Rain發表於2017-05-19

使用爬蟲爬取資料前,我們需要解析網站robots.txt檔案,以避免下載禁止爬取的url。這項工作需要使用python3自帶的urllib.robotparser模組

#使用爬蟲爬取資料前,我們需要解析網站robots.txt檔案

import urllib.robotparser
rp = urllib.robotparser.RobotFileParser()
rp.set_url("http://example.webscraping.com/robots.txt")
rp.read()
url = "http://example.webscraping.com"
user_agent = "BadCrawler"
print(rp.can_fetch(user_agent,url))

user_agent = "GoodCrawler"
print(rp.can_fetch(user_agent, url))
那麼我們將解析robots.txt這項功能整合在上一篇連結爬蟲裡,有了如下程式碼:

import urllib.request
import urllib.error 
import re #正規表示式
import urllib.parse #將url連結從相對路徑(瀏覽器可懂但python不懂)轉為絕對路徑(python也懂了)
import urllib.robotparser #爬取資料前解析網站robots.txt檔案,避免爬取網站所禁止或限制的
def download(url, user_agent = "brain", num_retries = 2):  #下載url網頁
    print("downloading:",url)
    header = {"user-agent": user_agent} #設定使用者代理,而不使用python預設的使用者代理Python-urllib/3.6
    req = urllib.request.Request(url, headers = header)
    try:
        html = urllib.request.urlopen(req).read()
    except urllib.error.URLError as e:    #下載過程中出現問題
        print("download error:",e.reason)
        html = None

        if num_retries > 0:     #錯誤4XX發生在請求存在問題,而5XX錯誤則發生在服務端存在問題,所以在發生5XX錯誤時重試下載
            if hasattr(e, "code") and 500<= e.code <600:
                return  download(url, user_agent, num_retries-1)  # recursively retry 5XX HTTP errors
    return html
#download("http://example.webscraping.com") #訪問正常
#download("http://httpstat.us/500") #這個網頁測試用,一直是5XXerror

#跟蹤連結的爬蟲:link_crawler()函式傳入兩個引數:要爬取的網站URL、用於跟蹤連結的正規表示式。
def link_crawler(seed_url, link_regex):
    """先下載 seed_url 網頁的原始碼,然後提取出裡面所有的連結URL,接著對所有匹配到的連結URL與link_regex 進行匹配,
如果連結URL裡面有link_regex內容,就將這個連結URL放入到佇列中,
下一次 執行 while crawl_queue: 就對這個連結URL 進行同樣的操作。
反反覆覆,直到 crawl_queue 佇列為空,才退出函式。"""
    crawl_queue = [seed_url]
    seen = set(crawl_queue) #有可能連結中互相重複指向,為避免爬取相同的連結,所以我們需要記錄哪些連結已經被爬取過(放在集合seen中),若已被爬取過,不再爬取
    while crawl_queue:
        url = crawl_queue.pop()
        
        rp = urllib.robotparser.RobotFileParser()   #爬取前解析網站robots.txt,檢查是否可以爬取網站,避免爬取網站禁止或限制的
        rp.set_url("http://example.webscraping.com/robots.txt")
        rp.read()
        user_agent = "brain" #這裡就是你爬取網站所使用的的代理
        if rp.can_fetch(user_agent, url):
            html = download(url)
            html = str(html)
            #filter for links matching our regular expression
            if html == None:
                continue
            for link in get_links(html):
                if re.match(link_regex, link):
                    link = urllib.parse.urljoin(seed_url, link) #把提取的相對url路徑link(view/178)轉化成絕對路徑(/view/Poland-178)link
                    if link not in seen:  #判斷是否之前已經爬取
                        seen.add(link) #之前沒有的話加在集合中以便後續繼續判斷
                        crawl_queue.append(link) #之前沒有的話這個連結可用,放在列表中繼續進行爬取
        else:
            print("Blocked by %s robots,txt" % url)
            continue
        
def get_links(html):
    """用來獲取一個html網頁中所有的連結URL"""
    #做了一個匹配模板 webpage_regex,匹配 <a href="xxx"> or <a href='xxx'>這樣的字串,並提取出裡面xxx的URL,請注意這裡的xxxURL很可能是原始碼中相對路徑,eg view/1 正常訪問肯定是打不開的
    webpage_regex = re.compile('<a href=["\'](.*?)["\']', re.IGNORECASE)
    return re.findall(webpage_regex,html)
    #return re.findall('<a[^>]+href=["\'](.*?)["\']', html)也可以這樣實現,但沒有上面的先編譯模板再匹配好                                                    

#只想找http://example.webscraping.com/index... or http://example.webscraping.com/view...
link_crawler("http://example.webscraping.com", "/(index|view)")



相關文章