爬蟲基礎——示例:登錄抽屜並點贊

   原理

1. 訪問抽屜,獲取獲取cookie (c1);cookie

2. 登錄時候,攜帶cookie (c1);session

3. 點贊時候,攜帶cookie (c1);post

每一次發送請求,請求頭必須設置 user-agenturl

   方法一

import requests

### 訪問 ###
r1 = requests.get(
    url="https://dig.chouti.com/",
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36}'
    },
)
r1_cookie = r1.cookies.get_dict()
print(r1_cookie)

### end ###

### 登錄 ###
post_dict = {
    'phone': '86xxxxx',
    'password': 'xxxxxx',
    'oneMonth': 1,
}
r2 = requests.post(
    url="https://dig.chouti.com/login",
    data=post_dict,
    headers= {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36}'
    },
    cookies=r1_cookie,
)

r2_cookie = r2.cookies.get_dict()  # 該cookie沒有用到
print(r2_cookie)
### end ##

### 點贊 ###
r3 = requests.post(
    url="https://dig.chouti.com/link/vote?linksId=20476281",
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36}'
    },
    cookies=r1_cookie
)

print(r3.text)

### end ###

 

   方法二

import requests


session = requests.Session()


### 訪問 ###
r1 = session.get(
    url="https://dig.chouti.com/",
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36}'
    },
)


### end ###

### 登錄 ###
post_dict = {
    'phone': '86xxxxxx',
    'password': 'xxxx',
    'oneMonth': 1,
}
r2 = session.post(
    url="https://dig.chouti.com/login",
    data=post_dict,
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36}'
    },
)

### end ##

### 點贊 ###
r3 = session.post(
    url="https://dig.chouti.com/link/vote?linksId=20487636",
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36}'
    },
)

print(r3.text)
相關文章
相關標籤/搜索