簡單的Crawler

相關(guān)知識

Reques庫

import requests

1.發(fā)送請求
使用Requests發(fā)送網(wǎng)絡(luò)請求非常簡單。

r = requests.get("http://www.lxweimin.com/u/3307ac591285")

其中r為Response對象。

2.傳遞URL參數(shù)
(沒有用到也看不懂)

3.響應(yīng)服務(wù)

r.text

Requests會自動解碼來自服務(wù)器的內(nèi)容,大多數(shù)unicode字符集都能被無縫地解碼。
當(dāng)請求發(fā)出后,Requests會基于HTTP頭部對響應(yīng)的編碼作出有根據(jù)的推測。當(dāng)訪問r.text時(shí),Requests會使用期推測的文本編碼。

4.二進(jìn)制響應(yīng)內(nèi)容
能以字節(jié)的方式訪問請求響應(yīng)體,對于非文本請求

r.content

Requests會自動為你解碼gazipdeflate傳輸編碼的響應(yīng)數(shù)據(jù)。
例如,以請求返回的二進(jìn)制數(shù)據(jù)創(chuàng)建一張圖片,可以使用如下代碼

from PIL import Image
from io import BytesIO

i = Image.open(BytesIO(r.content))
#直接展示照片,格式為.BMP
i.show() 
#保存為.jpg格式的照片
i.save('G:/1.jpg')

5....
(以后的內(nèi)容就用不上了,暫時(shí)不表)

以上引用 快速上手Requests

BeautifulSoup4庫

BeautifulSoup除了內(nèi)置HTML解析器,還支持一些第三方解析器,如html5lib,lxml等。
一般可以處理兩種html文件,一種是在線獲取再處理,一種是直接處理本地文件。

import requests
from bs4 import BeautifulSoup
#通過requests獲取
html = requests.get("http://www.lxweimin.com/u/3307ac591285")
soup = BeautifulSoup(html.text)
#處理本地文件
soup = BeautifulSoup(open('test.html'))

1.遍歷文檔樹
(我還沒學(xué)過html,理解不到這邊節(jié)點(diǎn)的劃分,就不貼了)
2.搜索文檔樹
find_all(name, attrs, recursive,text,**kwargs)
1)name參數(shù)可以查找所有名字為name的tag
查找所有<b>標(biāo)簽
A 字符串

soup.find_all('b')

B 正則表達(dá)式

import re
for tag in soup.find_all(re.compile('^b'))
  print(tag.name)

C 列表(不懂)

soup.find_all(['a', 'b'])

D 方法(打擾了)

2)關(guān)鍵字參數(shù)

#查找id為link2
soup.find_all(id = 'link2')
#傳入href參數(shù),搜索每個(gè)tag的'href'屬性
soup.find_all(href = re.compile('elseid'))
#可以使用多個(gè)指定名字的參數(shù),同時(shí)過濾tag的多個(gè)屬性
soup.find_all(href = re.compile('elseid'),id = 'link2')
#注意class是python中的關(guān)鍵字
soup.find_all('a',class_ = 'sister')
# 有些tag屬性在搜索不能使用,比如html5的data-*屬性,可以通過find_all()的attrs參數(shù)定義一個(gè)字典來搜索包含特殊屬性的tag
soup.find_all(attrs={'data-foo': 'value'})

3)text參數(shù)
通過text參數(shù)可以搜索文的字符串內(nèi)容與name參數(shù)的可選值一樣,text參數(shù)接受字符串,正則表達(dá)式,列表,True
4)limit參數(shù)
limit參數(shù)與SQL中的limit關(guān)鍵字類似,當(dāng)搜索到的結(jié)果數(shù)量達(dá)到limit的限制時(shí),就停止搜索返回結(jié)果

soup.find_all('a',limit = 2)

5)recursive參數(shù)
調(diào)節(jié)tag的find_all()方法時(shí),BeautifulSoup會檢索當(dāng)前所有的子孫節(jié)點(diǎn),如果只想搜索tag的直接子節(jié)點(diǎn),可以使用參數(shù)recursive = False

#為什么要加一個(gè).html???
soup.html.find_all('title', recursive = False)
find( name , attrs , recursive , text , **kwargs )
與 find_all() 方法唯一的區(qū)別是 find_all() 方法的返回結(jié)果是值包含一個(gè)元素的列表,而 find() 方法直接返回結(jié)果

引用-dreams512

4.CSS選擇器
寫css時(shí),標(biāo)簽名不加修飾,類名前加點(diǎn),id名前加#,可以利用類似的方法篩選元素,用到的方法是soup.select()返回list
(例子我也不懂,就不貼了)

參考別人的代碼

圖一
圖二

此段代碼還里遺留了五個(gè)問題【假裝是紅色】

#coding=utf-8
import requests
from bs4 import BeautifulSoup
import os
from multiprocessing import Pool

# http請求頭
# 這個(gè)頭是模仿瀏覽器訪問的,網(wǎng)站會根據(jù)這個(gè)判斷你的瀏覽器及操作系統(tǒng),很多網(wǎng)站沒有此信息會拒絕你訪問
Hostreferer = {
    'User-Agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
    'Referer': 'http://www.mzitu.com'
}
# 此請求頭破解盜鏈 --------------①
Picreferer = {
    'User-Agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
    'Referer': 'http://i.meizitu.net'
}

all_url = 'http://www.mzitu.com'

# 保存地址
path = 'G:/mzitu/'

def Download(href,title):
    html = requests.get(href,headers = Hostreferer)
    soup = BeautifulSoup(html.text,'html.parser')
    pic_max = soup.find_all('span')
    #這個(gè)10的意義------------------------------④
    #圖一
    pic_max = pic_max[10].text  # 最大頁數(shù)
    if(os.path.exists(path+title.strip().replace('?','')) and len(os.listdir(path+title.strip().replace('?',''))) >= int(pic_max)):
        print('已完畢,跳過'+title)
        return 1
    print("開始扒?。? + title)
    os.makedirs(path+title.strip().replace('?',''))
    os.chdir(path + title.strip().replace('?',''))

    for num in range(1,int(pic_max)+1):
        #圖二
        pic = href+'/'+str(num)

        html = requests.get(pic,headers = Hostreferer)

        mess = BeautifulSoup(html.text,"html.parser")

        pic_url = mess.find('img',alt = title)
        html = requests.get(pic_url['src'],headers = Picreferer)

        #-1的意義-------------------------------⑤
        file_name = pic_url['src'].split(r'/')[-1]

        f = open(file_name,'wb')
        f.write(html.content)
        f.close()
  #   print('完成'+title)


if __name__=='__main__':
    start_html = requests.get(all_url, headers=Hostreferer)

    # 找尋最大頁數(shù)
    soup = BeautifulSoup(start_html.text, "html.parser")
    page = soup.find_all('a', class_='page-numbers')
    #----------------------------②
    max_page = page[-2].text

    same_url = 'http://www.mzitu.com/page/'
    #多線程,此開了15線程
    pool = Pool(15)
    for n in range(1, int(max_page) + 1):
        #通過觀察不同頁碼的url得到的規(guī)律
        ul = same_url + str(n)
        #訪問每個(gè)頁面
        start_html = requests.get(ul, headers=Hostreferer)
        soup = BeautifulSoup(start_html.text, "html.parser")
        all_a = soup.find('div', class_='postlist').find_all('a', target='_blank')
        for a in all_a:
            print("a的內(nèi)容:")
            print(a)
            #<a  target="_blank"><img alt="性感女神易陽elly無圣光美圖 超級木瓜奶無比誘人" class="lazy" data-original="http://i.meizitu.net/thumbs/2018/06/137726_07a23_236.jpg" height="354" src="http://i.meizitu.net/pfiles/img/lazy.png" width="236"/></a>
            title = a.get_text()  # 提取文ben
            if (title != ''):
                print("a的get_text")
                print(title)
                #性感女神易陽elly無圣光美圖 超級木瓜奶無比誘人
                href = a['href']
                print('href:')
                print(href)
                #http://www.mzitu.com/137726
                #這是這個(gè)多線程的方法,不過這個(gè)傳參方式-----------------------③
                pool.apply_async(Download,args=(href,title))
    pool.close()
    pool.join()
    print('所有圖片已下完')

后注:

在粗略了解本段代碼之后就去嘗試爬 pexels的圖片,然后光榮犧牲。
不過在修改代碼的過程中,發(fā)現(xiàn)每個(gè)網(wǎng)站爬的難度是不一樣的,比如pexels,理論上在搜索之后,只需要在對當(dāng)前頁面進(jìn)行解析,就可以找到存儲圖片的URL鏈接。
當(dāng)時(shí)用Image.open(BytesIO(r.content))代碼,不能下載圖片,提示OSError: cannot identify image file <_io.BytesIO object at 0x00000153910C5620>,百度出來的解法不對,后來我試驗(yàn)了幾個(gè)網(wǎng)站找到了問題

import requests
from PIL import Image
from io import BytesIO

if __name__=='__main__':
    right200_url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1535279402&di=6594fb2bd0922a9f0d304818509723ea&imgtype=jpg&er=1&src=http%3A%2F%2Fa.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F0824ab18972bd4077557733177899e510eb3096d.jpg'
    wrong503_url = 'https://images.pexels.com/photos/157967/portrait-woman-girl-blond-157967.jpeg'
    wrong403_url = 'http://i.meizitu.net/2018/08/07b08.jpg'
    right200_url2 = 'https://visualhunt.com/photos/1/portrait-of-young-woman-wearing-sunglasses-and-holding-wildflowers-in-meadow.jpg'
    r = requests.get(wrong503_url)
    print(r)
    i = Image.open(BytesIO(r.content))
    i.show()
    i.save('G:/111.jpeg','JPEG')

發(fā)現(xiàn)有可能是服務(wù)器拒絕了你的請求,比如以上我的命名就是服務(wù)器返回值。
返回值的意義

在多次requests之后出現(xiàn)錯(cuò)誤

#requests.exceptions.SSLError: hostname 'requestb.in' doesn't match either of '*.herokuapp.com', 'herokuapp.com'
requests.get('https://requestb.in')

這是SSL證書驗(yàn)證的問題。SSL驗(yàn)證默認(rèn)是開啟的,如果證書驗(yàn)證是失敗,Requests會拋出SSLError。
可以通過開啟驗(yàn)證requests.get('https://github.com', verify=False)的方式解決。
當(dāng)時(shí)我也遇到這個(gè)問題,但原因好像不是這個(gè),我已經(jīng)搜索不到我的歷史記錄了,所以下一次遇到了再填坑。


requests官方中文‘皮’文檔
各瀏覽器的User-Agent

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容