Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从文件中使用随机代理/UA制作注释器?_Python_Python 2.7_Mechanize - Fatal编程技术网

Python 如何从文件中使用随机代理/UA制作注释器?

Python 如何从文件中使用随机代理/UA制作注释器?,python,python-2.7,mechanize,Python,Python 2.7,Mechanize,我正在尝试用python制作一个匿名器,但我不确定该使用哪个循环,因为你可以告诉我,我对这一切还很陌生 到目前为止,我有: import mechanize import cookielib br=mechanize.Browser() br.set_handle_robots(False) proxylist=open("/home/xyz/proxylist.txt","r+") ualist=open=("/home/xyz/ualist.txt","r+") def chan

我正在尝试用python制作一个匿名器,但我不确定该使用哪个循环,因为你可以告诉我,我对这一切还很陌生

到目前为止,我有:

import mechanize
import cookielib


br=mechanize.Browser()
br.set_handle_robots(False)

proxylist=open("/home/xyz/proxylist.txt","r+")
ualist=open=("/home/xyz/ualist.txt","r+")



def changeuseragent(useragent):
    br.addheaders=[('User-agent',useragent)]

def addproxy(proxy):
    br.set_proxies({"http":proxy})

def changecookie():
     cookie_jar = cookielib.LWPCookieJar()
     br.set_cookiejar(cookie_jar)

changeuseragent(useragent)
addproxy(proxy)
changecookie()
z=br.open("http://www.whatsmyuseragent.com")
print z.read()
在我打开上面两行文件之前,我有两行带有proxy和UserAgent值,所以我删除了它们,打开了两个文件,每个文件都有几个选项,每行是1个选项

我想要的是编写一个循环,这样每次我运行它时,它都会使用列表中的随机代理和用户代理访问网站。 我的主要问题是我不确定如何构建它,我是否应该使用whiletrue,或者是否应该尝试


要获取文件行,请使用:

lines = open(path,"r").readlines()
要从(比如)线阵列中选择随机元素,请执行以下操作:

import random #preferably at the top of the script
myline = random.choice(lines)
要删除行中有害的换行符和空格,请执行以下操作:

cleanline = line.strip()
要重复您的任务,请执行以下操作:

br=mechanize.Browser()
br.set_handle_robots(False)

def open_page(url,agent,proxy):
    changeuseragent(agent.strip()) # pass br here, or move above lines out
    addproxy(proxy.strip()) # into the global scope
    changecookie()

    return br.open(url)

# if script is executed, not imported. This line below is common magic.
if __name__=="__main__": 
    # TODO: open your files
    somelines = file(path,"r").readlines()
    #
    running = True
    while running:
        # TODO: select a line
        oneline = random.choice(lines)
        secondline = random.choice(otherlines)
        #
        f = open_page(your_url,agentline,proxyline)
        print f.read() #<or do whatever you wish
        f.close() #<not necessary

        running = raw_input("x and enter to exit: ").lower().startswith("x")
    # And on it goes.
编辑:我添加了一些伪代码。您需要修改或多或少明显的线条


关于主题:上面的循环在交互式控制台脚本中非常常见。

WOW-这似乎超出了我目前的理解范围><谢谢您尝试!刚刚看了你的编辑,睡了个好觉。。。。非常感谢,我相信我现在明白了!