爬蟲基本原理及Request和Response分析-創(chuàng)新互聯(lián)

一、爬蟲

成都創(chuàng)新互聯(lián)公司主要從事網(wǎng)頁設(shè)計(jì)、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、響應(yīng)式網(wǎng)站設(shè)計(jì)、程序開發(fā)、網(wǎng)站優(yōu)化、微網(wǎng)站、微信小程序等,憑借多年來在互聯(lián)網(wǎng)的打拼,我們?cè)诨ヂ?lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了豐富的網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站、網(wǎng)站設(shè)計(jì)、網(wǎng)絡(luò)營(yíng)銷經(jīng)驗(yàn),集策劃、開發(fā)、設(shè)計(jì)、營(yíng)銷、管理等多方位專業(yè)化運(yùn)作于一體。

互聯(lián)網(wǎng)是由網(wǎng)絡(luò)設(shè)備(網(wǎng)線,路由器,交換機(jī),防火墻等等)和一臺(tái)臺(tái)計(jì)算機(jī)連接而成,像一張網(wǎng)一樣。

互聯(lián)網(wǎng)的核心價(jià)值在于數(shù)據(jù)的共享/傳遞:數(shù)據(jù)是存放于一臺(tái)臺(tái)計(jì)算機(jī)上的,而將計(jì)算機(jī)互聯(lián)到一起的目的就是為了能夠方便彼此之間的數(shù)據(jù)共享/傳遞,否則就只能拿U盤去拷貝數(shù)據(jù)了。

互聯(lián)網(wǎng)中最有價(jià)值的便是數(shù)據(jù),爬蟲模擬瀏覽器發(fā)送請(qǐng)求->下載網(wǎng)頁代碼->只提取有用的數(shù)據(jù)->存放于數(shù)據(jù)庫或文件中,就得到了我們需要的數(shù)據(jù)了

爬蟲是一種向網(wǎng)站發(fā)起請(qǐng)求,獲取資源后分析并提取有用數(shù)據(jù)的程序。

二、爬蟲流程


1、基本流程介紹

發(fā)送請(qǐng)求-----> 獲取響應(yīng)內(nèi)容----->解析內(nèi)容 ----->保存數(shù)據(jù)

#1、發(fā)起請(qǐng)求
使用http庫向目標(biāo)站點(diǎn)發(fā)起請(qǐng)求,即發(fā)送一個(gè)Request
Request包含:請(qǐng)求頭、請(qǐng)求體等

#2、獲取響應(yīng)內(nèi)容
如果服務(wù)器能正常響應(yīng),則會(huì)得到一個(gè)Response
Response包含:html,json,圖片,視頻等

#3、解析內(nèi)容
解析html數(shù)據(jù):正則表達(dá)式,第三方解析庫如Beautifulsoup,pyquery等
解析json數(shù)據(jù):json模塊
解析二進(jìn)制數(shù)據(jù):以b的方式寫入文件

#4、保存數(shù)據(jù)
數(shù)據(jù)庫
文件

2、Request

常用的請(qǐng)求方式:GET,POST

其他請(qǐng)求方式:HEAD,PUT,DELETE,OPTHONS

>>> import requests
>>> r = requests.get('https://api.github.com/events')
>>> r = requests.post('http://httpbin.org/post', data = {'key':'value'})
>>> r = requests.put('http://httpbin.org/put', data = {'key':'value'})
>>> r = requests.delete('http://httpbin.org/delete')
>>> r = requests.head('http://httpbin.org/get')
>>> r = requests.options('http://httpbin.org/get')

百度搜索內(nèi)容爬取頁面:

import requests
response=requests.get("https://www.baidu.com/s",
                      params={"wd":"美女","a":1},
                      headers={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36"
                      })                #模擬在百度搜索美女的第一頁內(nèi)容,wd后面跟輸入的搜索內(nèi)容  #自己定制headers,解決網(wǎng)站的反爬蟲功能
print(response.status_code)
print(response.text)
with open("bd.html","w",encoding="utf8") as f:
    f.write(response.text)                        #下載的頁面寫進(jìn)bd.html文件,文件用瀏覽器打開發(fā)現(xiàn)和百度頁面一樣

3、Response

# 1、響應(yīng)狀態(tài)

200:代表成功

301:代表跳轉(zhuǎn)

404:文件不存在

403:權(quán)限

502:服務(wù)器錯(cuò)誤

# 2、Respone header

Location:跳轉(zhuǎn)

set - cookie:可能有多個(gè),是來告訴瀏覽器,把cookie保存下來

# 3、preview就是網(wǎng)頁源代碼

最主要的部分,包含了請(qǐng)求資源的內(nèi)容

如網(wǎng)頁html,圖片,二進(jìn)制數(shù)據(jù)等

# 4、response屬性


import requests
respone=requests.get('http://www.jianshu.com')
# respone屬性
print(respone.text)                     # 獲取響應(yīng)文本
print(respone.content)                 #獲取網(wǎng)頁上的二進(jìn)制圖片、視頻
print(respone.status_code)               #響應(yīng)狀態(tài)碼
print(respone.headers)                   #響應(yīng)頭

print(respone.cookies)                   #獲取cookies信息
print(respone.cookies.get_dict())
print(respone.cookies.items())

print(respone.url)
print(respone.history)                 #獲取history信息(頁面經(jīng)過重定向等方式,不是一次返回的頁面)
print(respone.encoding)                #響應(yīng)字符編碼

#關(guān)閉:response.close()
from contextlib import closing
with closing(requests.get('xxx',stream=True)) as response:
    for line in response.iter_content():
    pass

#5、獲取大文件


#stream參數(shù):一點(diǎn)一點(diǎn)的取,對(duì)于特別大的資源,一下子寫到文件中是不合理的
import requests
response=requests.get('https://gss3.baidu.com/6LZ0ej3k1Qd3ote6lo7D0j9wehsv/tieba-smallvideo-transcode/1767502_56ec685f9c7ec542eeaf6eac93a65dc7_6fe25cd1347c_3.mp4',
                      stream=True)
with open('b.mp4','wb') as f:
    for line in response.iter_content():           # 獲取二進(jìn)制流(iter_content)
        f.write(line)

三、爬取校花網(wǎng)視頻(加了并發(fā)的)

import requests         #安裝模塊 pip3 install requests
import re
import hashlib
import time
from concurrent.futures import ThreadPoolExecutor

pool=ThreadPoolExecutor(50)
movie_path=r'C:\mp4'

def get_page(url):
    try:
        response=requests.get(url)
        if response.status_code == 200:
            return response.text
    except Exception:
        pass
        
def parse_index(index_page):
    index_page=index_page.result()
    urls=re.findall('class="items".*?href="(.*?)"',index_page,re.S)       #找到所有屬性類為items的標(biāo)簽的鏈接地址,re.S表示前面的.*?代表所有字符
    for detail_url in urls:
        ret = re.search('<video.*?source src="(?P<path>.*?)"', res.text, re.S)   #找到所有video標(biāo)簽的鏈接地址
        detail_url = ret.group("path")
        res = requests.get(detail_url)
        if not detail_url.startswith('http'):
            detail_url='http://www.xiaohuar.com'+detail_url
        pool.submit(get_page,detail_url).add_done_callback(parse_detail)
        
def parse_detail(detail_page):
    detail_page=detail_page.result()
    l=re.findall('id="media".*?src="(.*?)"',detail_page,re.S)
    if l:
        movie_url=l[0]
        if movie_url.endswith('mp4'):
            pool.submit(get_movie,movie_url)
            
def get_movie(url):
    try:
        response=requests.get(url)
        if response.status_code == 200:
            m=hashlib.md5()
            m.update(str(time.time()).encode('utf-8'))
            m.update(url.encode('utf-8'))
            filepath='%s\%s.mp4' %(movie_path,m.hexdigest())
            with open(filepath,'wb') as f:                        #視頻文件,wb保存
                f.write(response.content)
                print('%s 下載成功' %url)
    except Exception:
        pass
        
def main():
    base_url='http://www.xiaohuar.com/list-3-{page_num}.html'
    for i in range(5):
        url=base_url.format(page_num=i)
        pool.submit(get_page,url).add_done_callback(parse_index)
        
if __name__ == '__main__':
    main()

四、爬蟲模擬登陸github網(wǎng)站


import requests
import re
# 第三次請(qǐng)求,登錄成功之后
    #- 請(qǐng)求之前自己先登錄一下,看一下有沒有referer
    #- 請(qǐng)求新的url,進(jìn)行其他操作
    #- 查看用戶名在不在里面
    
#第一次請(qǐng)求GET請(qǐng)求
response1 = requests.get(
    "https://github.com/login",
    headers = {
        "User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36",
    },
)
authenticity_token = re.findall('name="authenticity_token".*?value="(.*?)"',response1.text,re.S)
r1_cookies =  response1.cookies.get_dict()                   #獲取到的cookie

#第二次請(qǐng)求POST請(qǐng)求
response2 = requests.post(
    "https://github.com/session",
    headers = {
        "Referer": "https://github.com/",
        "User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36",
    },
    data={
            "commit":"Sign in",
            "utf8":"?",
            "authenticity_token":authenticity_token,
            "login":"zzzzzzzz",
            "password":"xxxx",
zhy..azjash2234
    },
    cookies = r1_cookies
)
print(response2.status_code)
print(response2.history)  #跳轉(zhuǎn)的歷史狀態(tài)碼

#第三次請(qǐng)求,登錄成功之后,訪問其他頁面
r2_cookies = response2.cookies.get_dict()           #拿上cookie,表示登陸狀態(tài),開始訪問頁面
response3 = requests.get(
    "https://github.com/settings/emails",
    headers = {
        "Referer": "https://github.com/",
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36",
    },
    cookies = r2_cookies,
)
print(response3.text)
print("zzzzzzzz" in response3.text)             #返回True說明就成功了

五、高級(jí)用法


1、SSL Cert Verification

import requests
respone=requests.get('https://www.12306.cn',
                     cert=('/path/server.crt','/path/key'))
print(respone.status_code)

2、使用代理

#官網(wǎng)鏈接: http://docs.python-requests.org/en/master/user/advanced/#proxies
#代理設(shè)置:先發(fā)送請(qǐng)求給代理,然后由代理幫忙發(fā)送(封ip是常見的事情)
import requests
proxies={
    'http':'http://egon:123@localhost:9743',#帶用戶名密碼的代理,@符號(hào)前是用戶名與密碼
    'http':'http://localhost:9743',
    'https':'https://localhost:9743',
}
respone=requests.get('https://www.12306.cn',proxies=proxies)
print(respone.status_code)
#支持socks代理,安裝:pip install requests[socks]
import requests
proxies = {
    'http': 'socks5://user:pass@host:port',
    'https': 'socks5://user:pass@host:port'
}
respone=requests.get('https://www.12306.cn',proxies=proxies)
print(respone.status_code)

3、超時(shí)設(shè)置


#兩種超時(shí):float or tuple
#timeout=0.1 #代表接收數(shù)據(jù)的超時(shí)時(shí)間
#timeout=(0.1,0.2)#0.1代表鏈接超時(shí)  0.2代表接收數(shù)據(jù)的超時(shí)時(shí)間
import requests
respone=requests.get('https://www.baidu.com', timeout=0.0001)

4、認(rèn)證設(shè)置


#官網(wǎng)鏈接:http://docs.python-requests.org/en/master/user/authentication/
#認(rèn)證設(shè)置:登陸網(wǎng)站是,彈出一個(gè)框,要求你輸入用戶名密碼(與alter很類似),此時(shí)是無法獲取html的
# 但本質(zhì)原理是拼接成請(qǐng)求頭發(fā)送
#         r.headers['Authorization'] = _basic_auth_str(self.username, self.password)
#看一看默認(rèn)的加密方式吧,通常網(wǎng)站都不會(huì)用默認(rèn)的加密設(shè)置

import requests
from requests.auth import HTTPBasicAuth
r=requests.get('xxx',auth=HTTPBasicAuth('user','password'))
print(r.status_code)

#HTTPBasicAuth可以簡(jiǎn)寫為如下格式
import requests
r=requests.get('xxx',auth=('user','password'))
print(r.status_code)

5、異常處理


import requests
from requests.exceptions import * #可以查看requests.exceptions獲取異常類型
try:
    r=requests.get('http://www.baidu.com',timeout=0.00001)
except ReadTimeout:
    print('===:')
# except ConnectionError:
#     print('-----網(wǎng)絡(luò)不通')
# except Timeout:
#     print('aaaaa')
except RequestException:
    print('Error')

6、上傳文件

import requests
files={'file':open('a.jpg','rb')}
respone=requests.post('http://httpbin.org/post',files=files)
print(respone.status_code)

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

網(wǎng)站標(biāo)題:爬蟲基本原理及Request和Response分析-創(chuàng)新互聯(lián)
URL標(biāo)題:http://www.muchs.cn/article2/ipgic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)站內(nèi)鏈網(wǎng)站制作、企業(yè)網(wǎng)站制作搜索引擎優(yōu)化、建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計(jì)公司