前提:csdn文章的浏览量还是比较好刷的,想要使用代理IP来刷文章,那就必须有一个一定数量、足够有效的代理ip库,没有一定数量的代理刷文章流浪量非常慢,所以首先就是要维护好自己的代理Ip库。我们都知道网站有很多的免费代理可以使用,既然是免费的那使用的效率肯定也是很低的,所以要想快速,稳定的刷文章阅读量最好是找一家稳定的代理商,比如某牛云的优质代理,是我使用过的所有代理里面最稳定和效果最好的,不管选择哪家代理都要进行测试对比。 思路:通过某牛云优质代理,利用requests库获取网页内容,get()方法请求携带proxies参数,通过bs4库解析网页 #coding=utf-8 import random import re import time import requests from bs4 import BeautifulSoup class ProxyHandler(object): def __init__(self): self.user_agent_list = [ "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1", "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6", "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1" ] # 获取代理地址的网址 self.proxy_get_url = 'www.16yun.cn' # 目标地址 self.visit_url = 'https://blog.csdn.net/t1623183652/article/details/73292150' # 获取能用的代理集合 self.proxy_list = [] # 请求超时时间为100s self.timeout = 100 def get_proxy_list(self): ''' 解析得到需要的代理列表数据 :return: ''' # 从self.user_agent_list中随机取出一个字符串 UA = random.choice(self.user_agent_list) print('随机产生的UA是====%s' % UA) headers = { 'User-Agent': UA } response = requests.get(url=self.proxy_get_url, headers=headers, timeout=self.timeout) html = response.text # 获取<td></td>里面的所有内容 list = re.findall(r'<td>(.*?)</td>', html) for index in range(int(len(list) / 6)): http = list[index * 6 + 3] ip = list[index * 6] port = list[index * 6 + 1] # 过滤掉一些socket连接 if re.search(r'(HTTP|HTTPS)', http) is None: continue proxy = '%s://%s:%s' % (http.lower(), ip, port) self.proxy_list.append(proxy) return (self.proxy_list) #80个代理ip #获取文章的阅读量 def readnum(self,url,header,proxy_ip): for i in range(5): time.sleep(60) #时间过小不能实现准确的刷阅读量(10) try: req = requests.get(url,headers =header,proxies=proxy_ip) soup = BeautifulSoup(req.text,'lxml') view = soup.select('#mainBox > main > div.blog-content-box > div.article-info-box > div > div > span')[0].getText() print("第%d次:阅读量%s" %(i+1,view)) except requests.exceptions.ConnectionError: print ("ConnectionError") if __name__ == '__main__': proxy_handler = ProxyHandler() proxy_list=proxy_handler.get_proxy_list() print (proxy_list) proxy_ip={ 'http':random.choice(proxy_list).split(r"//")[1] } url =('https://blog.csdn.net/relocy/article/details/51533302') header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'} proxy_handler.readnum(url,header,proxy_ip)
|