Home  >  Article  >  Backend Development  >  Python implements automatic blog browsing experience sharing

Python implements automatic blog browsing experience sharing

巴扎黑
巴扎黑Original
2017-08-08 15:58:362003browse

This article mainly introduces the example code of Python to automatically brush blog views. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look.

Source of ideas

By chance today, I heard others talking about the current “volume brushing” behavior, so I Piqued my curiosity. Then I looked at the requests module and it happened to be useful to me, so I wrote a simple test case. Miraculously, I discovered that this trick actually works. So what are you waiting for? Start brushing.

Prelude

The idea is very simple, just an implementation of sending a request, that’s it. The code is as follows:


headers = {
  'referer':'http://jb51.net/',
  'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36'
}

def getHtml(url,headers):
  req = urllib2.Request(url,headers=headers)
  page = urllib2.urlopen(req)
  html = page.read()
  return html

We just need to manually add the target URL and a header. Naturally, I will use my own to test it.

I tried running the code and it can indeed increase the number of views.

Slow growth

Since such a plan is feasible, it means that the idea is correct. So we naturally think of writing a loop. Wouldn’t this be able to achieve a massive increase in browsing?
Yes, I did. The code is as follows:


i= 0
while i < 10:
  url = 'http://jb51.net/marksinoberg/article/details/51501377'
  getHtml(url,headers)

At the beginning, you can clearly see the increase in the number of blogs, (^__^) Hee hee...it has begun to show results. But the good times didn't last long. I found that the number of views increased by 10 times. Just haha.

Then it cannot be added. It is estimated that the server has imposed certain restrictions on my access, otherwise it should be feasible.

Find every way to deal with the pit

As the saying goes, "There are policies from above, and countermeasures from below." Naturally, I couldn't be constrained by this, so I guessed that it was against my IP. Recorded. Then some limits were added to my number of visits.

My solution:

  1. Proxy IP for access: However, considering that there is no server, the proxy IP cannot be accessed. .

  2. Change IP: Since this is the case, then I will find a way to change my IP for access. So how do you change your IP? (I regret it now that I think about it. I didn’t pay attention to the computer network class at that time and didn’t learn IP spoofing well. Otherwise, it wouldn’t be able to be used now). But all roads lead to Rome, and I have other ways. As follows:

C:\Users\Administrator>ipconfig /release

Windows IP Configuration

Cannot perform any operations on the local connection, it has Disconnect media.

Wireless LAN Adapter Wireless Network Connection:

Connection-specific DNS suffix. . . . . . . :
Link-local IPv6 address. . . . . . . : fe80: :1d9f:d97b:fd16:1f6f%
Default gateway. . . . . . . . . . . . . :

Ethernet Adapter Local Area Connection:

Media status . . . . . . . . . . . . . : Media is down
Connection specific DNS suffix . . . . . . . : OurEDA.cn

Ethernet AdapterVMware Network Adapter VMnet1:

Connection-specific DNS suffix. . . . . . . :
Link-local IPv6 address. . . . . . . : fe80::359d:e81d:741:f257%1
IPv4 address . . . . . . . . . . . . . . : 192.168.229.1
Subnet mask . . . . . . . . . . . . . : 255.255.255.0
Default gateway. . . . . . . . . . . . . . . :

Ethernet AdapterVMware Network Adapter VMnet8:

Connection-specific DNS suffix. . . . . . . . :
Link-local IPv6 address. . . . . . . . . : fe80::94b1:d10f:b68:101d%1
IPv4 address. . . . . . . . . . . . : 192.168.244.1
Subnet mask . . . . . . . . . . . . . : 255.255.255.0
Default gateway . . . . . . . . . . . . . . :

Ethernet AdapterVirtualBox Host-Only Network:

Connection-specific DNS suffix. . . . . . . :
Link-local IPv6 address. . . . . . . : fe80::a5eb:545c:7d89:9451%
IPv4 address. . . . . . . . . . . . . . : 192.168.56.1
Subnet mask . . . . . . . . . . . . : 255.255.255.0
Default gateway. . . . . . . . . . . . . . :

Tunnel Adapter isatap.{4F399971-B739-4B71-BD79-E48233EEC9BE}:

Media Status . . . . . . . . . . . . : Media Disconnected
Connection specific DNS suffix. . . . . . . :

Tunnel Adapter isatap.{1860C94E-1007-4418-9A26-7D8AA8F06E15}:

Media Status . . . . . . . . . . . . . . : Media disconnected
Connection specific DNS suffix . . . . . . . :

Tunnel Adapter isatap.OurEDA.cn:

Media status . . . . . . . . . . . : Media disconnected
Connection specific DNS suffix. . . . . . . :

Tunnel Adapter isatap.dlut.edu .cn:

Media status . . . . . . . . . . . : Media disconnected
Connection specific DNS suffix. . . . . . . :

Tunnel adapter isatap.{6F7F27ED-942E-4EFB-ACF2-A4E8793B161D}:

   媒体状态  . . . . . . . . . . . . : 媒体已断开
   连接特定的 DNS 后缀 . . . . . . . :

C:\Users\Administrator>ipconfig /renew

Windows IP 配置

不能在 本地连接 上执行任何操作,它已断开媒体连接。

无线局域网适配器 无线网络连接:

   连接特定的 DNS 后缀 . . . . . . . :
   本地链接 IPv6 地址. . . . . . . . : fe80::1d9f:d97b:fd16:1f6f%12
   IPv4 地址 . . . . . . . . . . . . : 192.168.58.70
   子网掩码  . . . . . . . . . . . . : 255.255.252.0
   默认网关. . . . . . . . . . . . . : 192.168.56.1

以太网适配器 本地连接:

   媒体状态  . . . . . . . . . . . . : 媒体已断开
   连接特定的 DNS 后缀 . . . . . . . : OurEDA.cn

以太网适配器 VMware Network Adapter VMnet1:

   连接特定的 DNS 后缀 . . . . . . . :
   本地链接 IPv6 地址. . . . . . . . : fe80::359d:e81d:741:f257%14
   IPv4 地址 . . . . . . . . . . . . : 192.168.229.1
   子网掩码  . . . . . . . . . . . . : 255.255.255.0
   默认网关. . . . . . . . . . . . . :

以太网适配器 VMware Network Adapter VMnet8:

   连接特定的 DNS 后缀 . . . . . . . :
   本地链接 IPv6 地址. . . . . . . . : fe80::94b1:d10f:b68:101d%15
   IPv4 地址 . . . . . . . . . . . . : 192.168.244.1
   子网掩码  . . . . . . . . . . . . : 255.255.255.0
   默认网关. . . . . . . . . . . . . :

以太网适配器 VirtualBox Host-Only Network:

   连接特定的 DNS 后缀 . . . . . . . :
   本地链接 IPv6 地址. . . . . . . . : fe80::a5eb:545c:7d89:9451%16
   IPv4 地址 . . . . . . . . . . . . : 192.168.56.1
   子网掩码  . . . . . . . . . . . . : 255.255.255.0
   默认网关. . . . . . . . . . . . . :

隧道适配器 isatap.{4F399971-B739-4B71-BD79-E48233EEC9BE}:

   媒体状态  . . . . . . . . . . . . : 媒体已断开
   连接特定的 DNS 后缀 . . . . . . . :

隧道适配器 isatap.{1860C94E-1007-4418-9A26-7D8AA8F06E15}:

   媒体状态  . . . . . . . . . . . . : 媒体已断开
   连接特定的 DNS 后缀 . . . . . . . :

隧道适配器 isatap.OurEDA.cn:

   媒体状态  . . . . . . . . . . . . : 媒体已断开
   连接特定的 DNS 后缀 . . . . . . . :

隧道适配器 isatap.dlut.edu.cn:

   媒体状态  . . . . . . . . . . . . : 媒体已断开
   连接特定的 DNS 后缀 . . . . . . . :

隧道适配器 isatap.{6F7F27ED-942E-4EFB-ACF2-A4E8793B161D}:

   媒体状态  . . . . . . . . . . . . : 媒体已断开
   连接特定的 DNS 后缀 . . . . . . . :

是的,想必大家都看到了。核心的两个命令就是


// 更改路由表的配置
ipconfig / release // 释放网络,
ipconfig /renew  // 重新分配IP

这样基本上对于改变自己的IP是有点效果的。尤其是对局域网用户而言。

所以,我只需要在Python代码中调用系统的cmd命令,就可以动态的改变自己的IP了。也就达成了我的需求了

难题

虽然IP的问题解决了,但是这样刷的话,还是太慢。因为路由表的更新时需要时间的。而这和代码运行的速度比起来,真的是太慢太慢了。而且每次只能刷出十个浏览量,额。确实是比较的尴尬啊。费了这么大的劲,才刷了十个浏览量。怎么解决这个问题呢?

我其实也没有真正的解决这个问题,但是我发现这个限制也是不特别的强,因为我中途吃了个饭,回来的时候就发现原来的IP又可以刷了。大概45分钟左右的间隔吧!这是个突破点。
源码

其实思路很简单,就是想方设法的针对问题解决问题。不管对方的系统多么的坚固,也不可能是天衣无缝,总会有一种解决的办法的。下面是代码。


# coding:utf-8

#  __author__ = 'Mark sinoberg'
#  __date__ = '2016/5/26'
#  __Desc__ = 测试测试 刷新自己的博客的浏览量

import urllib2,re
from bs4 import BeautifulSoup

def getHtml(url,headers):
  req = urllib2.Request(url,headers=headers)
  page = urllib2.urlopen(req)
  html = page.read()
  return html

def parse(data):
  content = BeautifulSoup(data,'lxml')
  return content

def getReadNums(data,st):
  reg = re.compile(st)
  return re.findall(reg,data)

url = 'http://jb51.net/marksinoberg/article/details/51493318'
headers = {
  'referer':'http://jb51.net/',
  'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36'
}
i = 0
while i<24:
  html = getHtml(url,headers)
  content = parse(html)
  result = content.find_all('span',class_='link_view')
  print result[0].get_text()
  i = i +1

代码运行结果:


D:\Software\Python2\python.exe E:/Code/Python/MyTestSet/ulib2/AddWatcher.py
94人阅读
95人阅读
96人阅读
97人阅读
98人阅读
99人阅读
100人阅读
101人阅读
102人阅读
103人阅读
104人阅读
105人阅读
106人阅读
107人阅读
108人阅读
109人阅读
110人阅读
111人阅读
112人阅读
113人阅读
114人阅读
115人阅读
115人阅读
115人阅读

Process finished with exit code 0

比较好的地方就是使用了BeautifulSoup抓取了特定位置的数据,这里是对浏览量的抓取。从上面的结果也可以看出,一个IP抓取的数据量是有限制的,一般来说是10~30个,这里貌似是22次访问。

展望

其实我是可以做到一次运行多次刷新的效果的,但是这样做不是特别的正派,所以我就说一下自己的思路吧。

  1. Make a judgment on the result (views result). When two consecutive results are consistent, start python and execute the cmd command to update your IP. But this is a time-consuming operation. You can put it in a thread

  2. and then crawl your own blog list interface to get all your blog posts. Of course, simulated login is obviously used here. Then brush up the volume of each blog. Although this does not really solve the problem, it will have a good effect if a little adds up.

  3. Make a thread that regularly refreshes the amount and refresh it every XX time. At the end of the day, one article can probably get hundreds of visits. (I haven’t tried it and I don’t know)

The above is the detailed content of Python implements automatic blog browsing experience sharing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn