ITPub博客

首页 > > > python:教你用爬虫通过代理自动刷网页的浏览量

python:教你用爬虫通过代理自动刷网页的浏览量

作者:youou 时间:2019-08-27 11:52:15 0 删除 编辑

前言

每当看到几十万的阅读量的文章后面区区几个评论, 心中就在想, 难道大家现在都这么懒了吗? 但是有些文章阅读量不高, 评论为什么这么多呢?

我想这些应该都是刷的, 不光浏览量是刷的, 甚至那些无厘头的评论都是刷的。有人要问了, 你凭什么说别人刷的评论呢? 难道评论还可以刷? 我想说是的。

比如:我们在浏览微博的时候,会经常发现一件事, 怎么每个微博下面都有这个人的小广告呢? 难道这人都这么闲吗? 一个人闲也就罢了, 怎么这么多人都闲?可以在大量的微博下面评论大量的小广告, 并且点赞还非常多。具体是不是刷的,自己做判断就好了。

今天教大家的是, 用爬虫通过代理来增加网页的浏览量。以下是干货↓

思路

一、怎么获取大量的代理IP?

给大家推荐一个网站https://www.xicidaili.com/ , 看到下面的图片, 应该明白我们要做什么了吧。我们可以写一个爬虫去抓取这些IP。

但是, 在实际运行的时候, 发现了一个问题, 我怎么才能知道这些IP有用, 并且代理设置成功了呢? 当然, 也是有解决方案的,我们可以用代理访问这个网站http://httpbin.org/ip ,如果不正常会抛出异常, 如果是正常的就会返回如下信息

{

"origin": "106.38.91.34, 106.38.91.34"

}

废话不多说,直接上代码:

from bs4 import BeautifulSoup

import requests

import random

import time

def download_page(url):

print(url)

try:

#User Agent中文名为用户代理,简称 UA,它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版本、CPU 类型、浏览器及版本、

#浏览器渲染引擎、浏览器语言、浏览器插件等。

header = {

"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36"

}

data = requests.get(url,headers=header).text

except HTTPError as err:

print(err.__traceback__)

except ConnectionError as err:

print(err.__traceback__)

except TimeoutError as err:

print(err.__traceback__)

return data

#获取当前首页的内容

def parse_proxy_html(html):

try:

#解析器html.parser lxml xml html5lib

soup = BeautifulSoup(html, 'lxml')

find_all = soup.find_all('tr')

f = open("proxy", "w")

for find_tr in find_all:

find_tds = find_tr.find_all('td')

if len(find_tds) == 0:

continue

ip_temp = find_tds[1].contents[0]+":"+find_tds[2].contents[0]

try:

#随机模拟一个用户

user_agent = {'User-Agent':random.choice(agent_list)}

#随机获取一个代理ip

proxies = {'http':ip_temp}

#这样写没有问题,但是抓取出来的ip大都没有用

#判断代理ip是否有用,只保存有用的代理ip,两秒不响应就算无效的ip

targetUrl = 'http://httpbin.org/ip'

resp = requests.get(targetUrl, headers=user_agent,proxies=proxies,timeout=2)

print(resp.status_code)

print(resp.text)

print('~~有效的~~代理ip和端口号%s'%ip_temp)

f.write(ip_temp+"\n")

except Exception as e:

print('~~异常:%s'%e)

print('~~无效的~~代理ip和端口号%s'%ip_temp)

#获取下个页面地址

next_a = soup.find('a',attrs={'rel': 'next'})

if next_a is None:

print("~~None~~~")

return "";

return next_a['href']

except Exception as ex:

print('抓取信息异常:'+ex)

#首页数据抓取

HTTP = 'https://www.xicidaili.com'

def to_proxy_page():

download_url = '/wt/1'

num = 1

while download_url != 'javascript:;' and download_url!='':

if num == 3:

print('~~~~够用了~~不抓取了~~~')

break

print("第%d次请求地址:%s"%(num,download_url));

#随机停顿几秒

i = random.randint(1, 3)

time.sleep(i)

#获取页面信息

html = download_page(HTTP + download_url)

#解析和保存

download_url = parse_proxy_html(html)

num = num+1;

if __name__ == '__main__':

to_proxy_page()

获取到的代理地址格式如下(想要多少, 代码自己设置下就好了):

163.204.243.4:9999

60.13.42.248:9999

二、通过代理地址去刷新网页

可以先写一个User-Agent列表, 模拟不同的浏览器信息。然后配置需要刷新的网页就好了。

import random

import socket

import requests

#定义需要刷新的页面集合

refreshPage_list = [

#1062

'https://blog.csdn.net/dujianxiong/article/details/80844652',

#153

'https://blog.csdn.net/super_DuoLa/article/details/90205512'

]

#定义User-Agent集合

agent_list = [

#Win7:

'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1',

#Win7:

'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0',

#Win7:

'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',

#Win7:

'Opera/9.80 (Windows NT 6.1; U; zh-cn) Presto/2.9.168 Version/11.50',

#Win7+ie9:

'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; Tablet PC 2.0; .NET4.0E)',

#Win7+ie8:

'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3)',

#WinXP+ie8:

'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB7.0)',

#WinXP+ie7:

'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',

#WinXP+ie6:

'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)',

#傲游3.1.7在Win7+ie9,高速模式:

'Mozilla/5.0 (Windows; U; Windows NT 6.1; ) AppleWebKit/534.12 (KHTML, like Gecko) Maxthon/3.0 Safari/534.12',

#傲游3.1.7在Win7+ie9,IE内核兼容模式:

'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E)',

#搜狗3.0在Win7+ie9,IE内核兼容模式:

'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E; SE 2.X MetaSr 1.0)',

#搜狗3.0在Win7+ie9,高速模式:

'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.33 Safari/534.3 SE 2.X MetaSr 1.0',

#360浏览器3.0在Win7+ie9:

'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E)',

#QQ浏览器6.9(11079)在Win7+ie9,极速模式:

'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1 QQBrowser/6.9.11079.201',

#QQ浏览器6.9(11079)在Win7+ie9,IE内核兼容模式:

'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E) QQBrowser/6.9.11079.201',

#阿云浏览器1.3.0.1724 Beta(编译日期2011-12-05)在Win7+ie9:

'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)'

]

#开始搞

def to_refresh_page():

try:

#获取代理信息

f = open("proxy",'r')

lines = f.readlines()

proxys = []

for line in lines:

ip = line.strip()

proxys.append(ip)

#每个页面刷新10次

for i in range(10):

for url in refreshPage_list:

#随机模拟一个用户

user_agent = {'User-Agent':random.choice(agent_list)}

#随机获取一个代理ip

proxies = {'http':random.choice(proxys)}

r = requests.get(url,headers=user_agent,proxies=proxies)

print(r)

print('刷新成功,请求地址url:%s返回状态status:%s'%(url,r.status_code))

except Exception as e:

print('刷新页面异常:%s'%e)

if __name__ == '__main__':

to_refresh_page()

代码中我加了很多注解,就不长篇大论了,如果有不明白的可以评论。

三、思考问题

在实际测试中,发现刚才还有效的代理IP,当去刷新网页的时候就链接超时了呢?为什么请求了很多次,网页的浏览量并没有涨太多呢?

注: 文章所写代码仅用于技术交流

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4729/viewspace-3120239/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-07-17

  • 博文量
    294
  • 访问量
    13386