如果博主想提高自己博客的访问量,可以写一个自动访问的程序。需要考虑到以下几点:
1、如果博主文章较多,则要考虑分页的情况;
2、如果只想访问文章,则要利用正则表达式筛选链接;
3、如果发现访问的文章多于自己发表的文章,则要去重;
4、如果自动访问期间进行翻墙操作,自动访问将停止;
import requests
import re
import time
headers = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
"Cookie": "l=AurqcPuigwQdnQv7WvAfCoR1OlrRQW7h; isg=BHp6mNB79CHqYXpVEiRteXyyyKNcg8YEwjgLqoRvCI3ddxqxbLtOFUBGwwOrZ3ad; thw=cn; cna=VsJQERAypn0CATrXFEIahcz8; t=0eed37629fe7ef5ec0b8ecb6cd3a3577; tracknick=tb830309_22; _cc_=UtASsssmfA%3D%3D; tg=0; ubn=p; ucn=unzbyun; x=e%3D1%26p%3D*%26s%3D0%26c%3D0%26f%3D0%26g%3D0%26t%3D0%26__ll%3D-1%26_ato%3D0; miid=981798063989731689; hng=CN%7Czh-CN%7CCNY%7C156; um=0712F33290AB8A6D01951C8161A2DF2CDC7C5278664EE3E02F8F6195B27229B88A7470FD7B89F7FACD43AD3E795C914CC2A8BEB1FA88729A3A74257D8EE4FBBC; enc=1UeyOeN0l7Fkx0yPu7l6BuiPkT%2BdSxE0EqUM26jcSMdi1LtYaZbjQCMj5dKU3P0qfGwJn8QqYXc6oJugH%2FhFRA%3D%3D; ali_ab=58.215.20.66.1516409089271.6; mt=ci%3D-1_1; cookie2=104f8fc9c13eb24c296768a50cabdd6e; _tb_token_=ee7e1e1e7dbe7; v=0",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64;` rv:47.0) Gecko/20100101 Firefox/47.0"}
# 获取当前页面的全部博客链接
def getUrls(url):
# 发送请求
resp = requests.request('GET', url, data='', headers=headers)
# 设置解码方式
resp.encoding=resp.apparent_encoding
# 这里会用设置的解码方式解码
html_source = resp.text
# 正则表达式,博客链接中不含>;"',并以数字结尾
# []:字符类,字符类在匹配时如果没有指定数量则只会匹配其中的一个
# ^:紧随左方括号,表示否定一个字符类
# *:匹配前面的字符0次或多次
# \d:匹配一个Unicode数字
urls = re.findall('https://[^>;\"\']*\d',html_source)
new_urls=[]
for url in urls:
if 'wxsy024680/article/details' in url:
new_urls.append(url)
return new_urls
# 获取各个分页的博客链接,包括热门文章、最新文章、最新评论
pages = 3
urls = []
for i in range(1, pages+1):
urlname = 'https://blog.csdn.net/wxsy024680/article/list/' + '{}'.format(i)
urls += getUrls(urlname)
# 博客链接去重,因为热门文章与最新文章都来自于发表文章
new_urls = list(set(urls))
new_urls.sort(key=urls.index)
# 循环访问全部博客链接
epoch = 0
total = len(new_urls)
while True:
for i, url in enumerate(new_urls):
requests.request("GET", url, data='', headers=headers)
print('第{}轮 {}/{}:{}'.format(epoch, i, total, url))
time.sleep(5)
time.sleep(5)
epoch += 1
运行结果: