1. 程式人生 > >python爬蟲之requests模塊

python爬蟲之requests模塊

.post 過大 form表單提交 www xxxxxx psd method date .com

一. 登錄事例

a. 查找汽車之家新聞 標題 鏈接 圖片寫入本地

技術分享
import requests
from bs4 import BeautifulSoup
import uuid

response = requests.get(
    http://www.autohome.com.cn/news/
)
response.encoding = gbk
soup = BeautifulSoup(response.text,html.parser)        # HTML會轉換成對象
tag = soup.find(id=auto-channel-lazyload-article
) li_list = tag.find_all(li) for i in li_list: a = i.find(a) if a: print(a.attrs.get(href)) txt = a.find(h3).text print(txt) img_url = txt = a.find(img).attrs.get(src) print(img_url) img_response = requests.get(url=img_url) file_name
= str(uuid.uuid4()) + .jpg with open(file_name,wb) as f: f.write(img_response.content)
用到BeautifulSoup模塊尋找標簽

b. 抽屜點贊 獲取頁面和登錄都會獲取gpsd 點贊會使用獲取頁面的gpsd 而不是登錄的gpsd

技術分享
import requests

#先獲取頁面

r1 = requests.get(http://dig.chouti.com/)
r1_cookies = r1.cookies.get_dict()

#登錄
post_dict 
= { "phone":"8615131255089", "password":"woshiniba", "oneMonth":"1" } r2 = requests.post( url="http://dig.chouti.com/login", data = post_dict, cookies=r1_cookies ) r2_cookies = r2.cookies.get_dict() # 訪問其他頁面 r3 = requests.post( url="http://dig.chouti.com/link/vote?linksId=13921091", cookies={gpsd:r1_cookies[gpsd]} ) print(r3.text)
抽屜網頁面的(gpsd)

c. 登錄githup 攜帶cookie登錄

技術分享
import requests
from bs4 import BeautifulSoup

r1 = requests.get(https://github.com/login)
s1 = BeautifulSoup(r1.text,html.parser)

# 獲取csrf_token
token = s1.find(name=input,attrs={name:"authenticity_token"}).get(value)
r1_cookie_dict = r1.cookies.get_dict()

# 將用戶名 密碼 token 發送到服務端 post
r2 = requests.post(
    https://github.com/session,
    data={
    commit:Sign in,
    utf8:?,
    authenticity_token:token,
    login:[email protected],
    password:alex3714
    },
    cookies=r1_cookie_dict
)

#  獲取登錄後cookie
r2_cookie_dict = r2.cookies.get_dict()

#合並登錄前的cookie和登錄後的cookie
cookie_dict = {}
cookie_dict.update(r1_cookie_dict)
cookie_dict.update(r2_cookie_dict)




r3 = requests.get(
    url=https://github.com/settings/emails,
    cookies=cookie_dict
)

print(r3.text)
View Code

二. requests 參數

- method:  提交方式
            - url:     提交地址
            - params:  在URL中傳遞的參數,GET
            - data:    在請求體裏傳遞的數據
            - json     在請求體裏傳遞的數據
            - headers  請求頭
            - cookies  Cookies
            - files    上傳文件
            - auth     基本認知(headers中加入加密的用戶名和密碼)
            - timeout  請求和響應的超市時間
            - allow_redirects  是否允許重定向
            - proxies  代理
            - verify   是否忽略證書
            - cert     證書文件
            - stream   村長下大片
            - session: 用於保存客戶端歷史訪問信息

a. file 發送文件

技術分享
import requests

requests.post(
    url=xxx,
    filter={
        name1: open(a.txt,rb),   #名稱對應的文件對象
        name2: (bbb.txt,open(b.txt,rb))     #表示上傳到服務端的名稱為 bbb.txt
    }
)
View Code

b. auth 認證

#配置路由器訪問192.168.0.1會彈出小彈窗,輸入用戶名,密碼 點擊登錄不是form表單提交,是基本登錄框,這種框會把輸入的用戶名和密碼 經過加密放在請求頭發送過去
技術分享
import requests

requests.post(
    url=xxx,
    filter={
        name1: open(a.txt,rb),   #名稱對應的文件對象
        name2: (bbb.txt,open(b.txt,rb))     #表示上傳到服務端的名稱為 bbb.txt
    }
)
View Code

c. stream 流

技術分享
#如果服務器文件過大,循環下載

def param_stream():
    ret = requests.get(http://127.0.0.1:8000/test/, stream=True)
    print(ret.content)
    ret.close()

    # from contextlib import closing
    # with closing(requests.get(http://httpbin.org/get, stream=True)) as r:
    # # 在此處理響應。
    # for i in r.iter_content():
    # print(i)
View Code

d. session 和django不同 事例:簡化抽屜點贊

技術分享
    import requests

    session = requests.Session()

    ### 1、首先登陸任何頁面,獲取cookie

    i1 = session.get(url="http://dig.chouti.com/help/service")

    ### 2、用戶登陸,攜帶上一次的cookie,後臺對cookie中的 gpsd 進行授權
    i2 = session.post(
        url="http://dig.chouti.com/login",
        data={
            phone: "8615131255089",
            password: "xxxxxx",
            oneMonth: ""
        }
    )

    i3 = session.post(
        url="http://dig.chouti.com/link/vote?linksId=8589623",
    )
    print(i3.text)
View Code

python爬蟲之requests模塊