python項目之 增加博客訪問量

python項目之 增加博客訪問量

爲什麼要有?

寫博客的訪問量太低,沒動力繼續寫,需要刷榜增加人氣。
當然了,學技術爲主,你懂的!

需準備文件

代理的ip地址

寫成txt文件,格式

http://11.11.11.11:8888

博客的地址

寫成txt文件

博客標題,下面是博客地址
http://XXXXXXXX

思路

模擬瀏覽器登錄,爲了防止被服務器堵IP,還是用到了代理IP地址。

改進

後續再把瀏覽器信息擴展就完美了。

源碼如下

# coding = utf-8
####################################################
# coding by 劉雲飛
####################################################
import requests
import re
import time
import random
from random import choice

ips = []
blog_ads = []

with open('ip.txt', 'r') as f:
    lines = f.readlines()
    for line in lines:
        ip_one = line.strip()
        ips.append(ip_one)

with open('blogdizhi.txt', 'r', encoding='utf-8') as f2:
    lines = f2.readlines()
    line_num = len(lines)
    pag = 0
    for line in lines:
        pag += 1
        if pag % 2 == 0:
            ads = line.strip()
            blog_ads.append(ads)
        else:
            ads = line.strip()

headers = {
    'Host': 'blog.csdn.net',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/42.0',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
    'Accept-Encoding': 'gzip, deflate',
    'Referer': 'http://www.google.com',
    'Connection': 'keep-alive',
    'Cache-Control': 'max-age=0',
}

i = 0
j = 0
while i < 548:
    blog = random.choice(blog_ads)
    ip_i = random.choice(ips)
    proxies = {'http': ip_i}
    print(blog)
    print(proxies)
    i += 1
    print("now trying " + str(i) + ',failed ' + str(j))
    sleep_time = random.randint(5, 23)
    print('------>sleep ' + str(sleep_time) + " s")
    time.sleep(sleep_time)
    session = requests.session()
    try:
        response = session.get(blog, headers=headers, proxies=proxies)
        str_code = response.status_code
        print('>>>>>' + str(str_code))
        if str_code > 200:
            j += 1
    except:
        print('time out')
        j += 1
        pass
    session.close()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章