【日常】利用python刷CSDN访问量

最近刚刚进村写博客,精心创作却苦于无人阅览。于是耍了点小聪明准备写个小爬虫给自己的博客刷刷访问量。

以下代码是对《【项目记录】雪球网股票组合数据爬虫(包括雪球模拟登录代码)》这篇博客(博客链接:https://blog.csdn.net/CY19980216/article/details/82770410)进行的不断访问以提高访问量,由于代码非常浅显易懂,我就不多加赘述,大家觉得有用就拿去用。只有一个问题要提一下,访问间隔60秒是我试出来的较优请求时间间隔,低于60秒访问一次是无法实现每请求一次提高一个访问量的。

代码自取 ↓↓↓

# -*- coding:UTF-8 -*-
"""
	作者:囚生CY
	平台:CSDN
	时间:2018/09/23
	转载请注明原作者
	创作不易,仅供分享
"""

import re
import time
import random
import requests
import urllib.request
from bs4 import BeautifulSoup

firefoxHead = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0"}
IPRegular = r"(([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5]).){3}([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])"
host = "https://blog.csdn.net"
url = "https://blog.csdn.net/CY19980216/article/details/{}"
codes = ["82770410","82825833"]

def parseIPList(url="http://www.xicidaili.com/"):
	IPs = []
	request = urllib.request.Request(url,headers=firefoxHead)
	response = urllib.request.urlopen(request)
	soup = BeautifulSoup(response,"lxml")								 
	tds = soup.find_all("td")
	for td in tds:
		string = str(td.string)
		if re.search(IPRegular,string):
			IPs.append(string)
	return IPs

def PV(code):
	s = requests.Session()
	s.headers = firefoxHead
	count = 0
	while True:
		count += 1
		print("正在进行第{}次访问\t".format(count),end="\t")
		IPs = parseIPList()
		s.proxies = {"http":"{}:8080".format(IPs[random.randint(0,40)])}
		s.get(host)
		r = s.get(url.format(code))
		html = r.text
		soup = BeautifulSoup(html,"html.parser")
		spans = soup.find_all("span")
		print(spans[2].string)
		time.sleep(random.randint(60,75))	

def main():
	PV(codes[0])


if __name__ == "__main__":
	main()

运行结果如图所示 ↓↓↓

【日常】利用python刷CSDN访问量_第1张图片

分享学习,共同进步!

 

你可能感兴趣的:(python,日常,小确幸)