我正在编写一个代码来获取域名列表的Google Pagerank。经过一段时间和一定数量的查询,我的 ip 被列入黑名单,现在我收到 HTTP 错误 403。我的问题是,如何添加到我的脚本中,每隔几秒钟从我正在使用的测量服务器上的 IP 池中更改一次 IP 地址。有人可以帮忙吗?
from __future__ import print_function, division
import sys
import urllib.request as _urlib1 # py3
import urllib.parse as _urlib2 # py 3
def get_pagerank(url):
hsh = check_hash(hash_url(url))
gurl = 'http://toolbarqueries.google.com/tbr?client=navclient-auto&features=Rank&ch=%s&q=info:%s' % (hsh, _urlib2.quote(url))
u = _urlib1.urlopen(gurl)
s = u.read().decode('utf-8') # for py2, comment .decode() part
#print(s) # debug - response of server
rank = s.strip()[9:]
if rank == '':
rank = 'None'
if rank == 'None':
rank = 'None'
return rank
这是我的代码:
在 Linux 下,对于一个接口 eth0 :
sudo ifconfig eth0 down
sudo ifconfig eth0 newAdress
sudo ifconfig eth0 up
您必须将其作为来自python的命令启动,然后从可用IP列表中更改newAdress。
您可以使用以下命令运行命令:
from subprocess import call
subprocess.call(["command", "arg1", "arg2"])
为了在周期性时间执行此操作,请使用线程或芹菜