天天看點

python爬蟲執行個體源碼_Python資料分析及可視化執行個體之爬蟲源碼(04)

python爬蟲執行個體源碼_Python資料分析及可視化執行個體之爬蟲源碼(04)

系列文章總目錄:

yeayee:Python資料分析及可視化執行個體目錄​zhuanlan.zhihu.com

python爬蟲執行個體源碼_Python資料分析及可視化執行個體之爬蟲源碼(04)
python爬蟲執行個體源碼_Python資料分析及可視化執行個體之爬蟲源碼(04)

1.背景介紹

(1)接上一彈留下的作業,使用Cookies免密碼登入今日頭條,并針對上一節采集的URL自動POST一條回複,經測試,今日頭條的回複頻率過快(3條),送出按鈕就挂了。是以,本文旨在說明如何進行POST,Login登入也是一個道理。

python爬蟲執行個體源碼_Python資料分析及可視化執行個體之爬蟲源碼(04)

在Code中設定Cookie時,要帶上Host(如果有)。

(2)回複内容也是有技巧的,在本案例中回複内容由‘标題’,‘關鍵詞’,及宣傳廣告語組成,避免被機器人認定為重複内容。

python爬蟲執行個體源碼_Python資料分析及可視化執行個體之爬蟲源碼(04)

呵呵,别以為找了POST_URL然後post Data就可以搞定回複!可以明确的說,行不通。因為在送出釋出按鈕的時候,還在加載了幾個連結,作用就是改變了Session中的Cookie。是以,在用Requests的時候,還的模拟前面的Get請求,自動更新Session。比如本案例中還需要Get請求:http://www.toutiao.com/user/info/

python爬蟲執行個體源碼_Python資料分析及可視化執行個體之爬蟲源碼(04)
python爬蟲執行個體源碼_Python資料分析及可視化執行個體之爬蟲源碼(04)

PS:也不知道他們的程式猿為什麼要将Post資料重複成兩個變量送出給資料庫?

(3)沒錯,現在可以成功Post。

擴充一下:

登陸網站的POST參數相對難擷取,尤其是一些JS動态生成的參數,這個時候就要用到PhantomJS。

再次推薦xchaoinfo/fuck-login

,該有的Fuck都有了,沒有的也可以照貓畫虎,我就不專門講Post登陸,用Cookie單賬号登陸采集資料就夠大部分人用了。更多進階黑的操作,牽扯到灰産和惡意爬蟲,不便詳述(如更換IP,更換ID,更換IQ等)。

(4)下一彈是講多線程,多程序呢?還是繼續祭出神器Phantoms給度娘來一發?看客們留言哈!!!

2.源碼

# coding = utf-8

import requests
import re, json
from bs4 import BeautifulSoup
import time
headers = {
    'Host': 'www.toutiao.com',
    'content-type': 'application/json',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
    'Cookie': 'tt_webid=646855411836120***8; 。。。。不要随便讓人看到你的小秘密',
    'Connection': 'keep-alive'
}

s = requests.session()


def post_data(base_url,post_content,post_id):
    try:
        # base_url = 'http://toutiao.com/group/64689424888533888/'
        url2 = 'http://www.toutiao.com/user/info/'
        content = s.get(url2, headers=headers) # 擷取Useinfog,更新session
        # soup = BeautifulSoup(content, "lxml")
        # print(soup.prettify())
        headers['Referer'] = base_url
        url3 = 'http://www.toutiao.com/api/comment/post_comment/'
        data = {
            'status':post_content,
            'content': post_content,
            'group_id':post_id,
            'item_id':post_id

        }
        s.post(url3, headers=headers, data=data)  # 評論文章
        print('評論成功啦,嚯嚯')
    except:
        print('掉坑裡了,爬起來')
        pass

f_lines = open('sorted.txt','r',encoding='utf-8').readlines()
posted_urls = open('posted.txt','r',encoding='utf-8').read()
# print(f_lines[0].strip().split(','))  # 實作記錄已評論的Url,中斷後可以接着評論
for f_line in  f_lines:
    if 'http://toutiao.com/group/' in f_line:  # 說明是可以評論的文章
        line_list = f_line.strip().split(',')
        base_url = line_list[1]
        print(base_url)
        post_content = '大神,你發的《'+ line_list[2]+'》很有借鑒意義,能否轉發呢?'
        # print(post_content)
        post_id = base_url.split('/')[-2]
        if base_url  not in posted_urls :  # 進入下一個循環
            try:
                time.sleep(3)
                post_data(base_url,post_content,post_id)
                f_posted = open('posted.txt','a',encoding='utf-8')
                f_posted.write(base_url+'n')
                f_posted.close()
            except:
                print('又他媽掉坑裡了,爬起來')
                pass

        else:
            print('曾經評論過了')
           

yeayee:Python資料分析及可視化執行個體目錄​zhuanlan.zhihu.com

python爬蟲執行個體源碼_Python資料分析及可視化執行個體之爬蟲源碼(04)
python爬蟲執行個體源碼_Python資料分析及可視化執行個體之爬蟲源碼(04)

最後,别隻收藏不關注哈