Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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 如何在此命令中添加代理列表_Python - Fatal编程技术网

Python 如何在此命令中添加代理列表

Python 如何在此命令中添加代理列表,python,Python,我有一些python代码,代码是访问站点然后退出 现在我需要的是,我想知道我们是如何添加代理列表的,以及添加代理列表的命令是什么。 我有一个代理列表,其中包含一组IP和端口,我希望脚本能够逐个读取它们 proxylist.txt示例: 111.68.103.39:3128 83.246.226.42:8080 196.20.65.211:8080 203.91.39.23:8080 110.34.39.58:8080 24.64.94.112:8080 190.192.125.141:8080

我有一些python代码,代码是访问站点然后退出 现在我需要的是,我想知道我们是如何添加代理列表的,以及添加代理列表的命令是什么。 我有一个代理列表,其中包含一组IP和端口,我希望脚本能够逐个读取它们

proxylist.txt示例:

111.68.103.39:3128
83.246.226.42:8080
196.20.65.211:8080
203.91.39.23:8080
110.34.39.58:8080
24.64.94.112:8080
190.192.125.141:8080
122.155.13.14:8001
200.54.92.187:3128
62.84.13.33:8080
200.80.30.155:3128
190.95.246.3:3128
62.97.116.178:443
所有这些代理都被添加到一个文件中,并命名为proxylist.txt。现在我想将它们添加到下面给出的脚本中:

#!/usr/bin/env python

#Disable some warnings
import logging
logging.getLogger("mechanize").setLevel(logging.ERROR)
import mechanize

# Setup some variables
url = 'http://www.google.com/'
proxy = "proxylist.txt"
ua = 'Mozilla/5.0 (X11; Linux i686 on x86_64; rv:8.0) Gecko/20100101 Firefox/8.0'

# Setup the browser instance
br = mechanize.Browser()
br.addheaders = [('User-agent', ua)]
br.set_handle_gzip(True)
br.set_handle_equiv(True)
br.set_handle_referer(True)
br.set_handle_robots(False)

# Configure the proxy

proxylist = proxy.split(':')
proxytype = proxylist[0]
proxyserv = proxylist[1]
proxyport = proxylist[2]
proxyline = proxyserv + ':' + proxyport
proxydict = {proxytype: proxyline}
br.set_proxies(proxydict)

# Get the URL
print 'Retreiving the URL ' + url + '...'

# Get the returned HTML
html = response.read()

# Close the browser instance
br.close()

# Print the HTML
print html
文件必须像循环一样逐个通过每个代理

proxydict= {} # create an empty dict
with open('proxylist.txt') as proxylist: # open the proxylist file
    for line in proxylist: # iterate through all lines in the file
        proxytype, proxyserv, proxyport = line.split(':') # extract proxy type, ip and port
        proxydict[proxytype]= proxyserv + ':' + proxyport # add the proxy to the dict
所以完整的代码是

#!/usr/bin/env python

#Disable some warnings
import logging
logging.getLogger("mechanize").setLevel(logging.ERROR)
import mechanize

# Setup some variables
url = 'http://www.google.com/'
proxy = "proxylist.txt"
ua = 'Mozilla/5.0 (X11; Linux i686 on x86_64; rv:8.0) Gecko/20100101 Firefox/8.0'

# Setup the browser instance
br = mechanize.Browser()
br.addheaders = [('User-agent', ua)]
br.set_handle_gzip(True)
br.set_handle_equiv(True)
br.set_handle_referer(True)
br.set_handle_robots(False)

# Configure the proxy

proxydict= {} # create an empty dict
with open('proxylist.txt') as proxylist: # open the proxylist file
    for line in proxylist: # iterate through all lines in the file
        proxytype, proxyserv, proxyport = line.split(':') # extract proxy type, ip and port
        proxydict[proxytype]= proxyserv + ':' + proxyport # add the proxy to the dict
br.set_proxies(proxydict)

# Get the URL
print 'Retreiving the URL ' + url + '...'

# Get the returned HTML
html = response.read()

# Close the browser instance
br.close()

# Print the HTML
print html

所以基本上你的问题是如何逐行读取文件的内容?是的,命令必须能够加载浏览器和proxylistRead中的每个代理。我只是一个初学者,所以你能按照我想要的方式重新编码吗?因为我不理解你给出的链接。我运行了这个代码,得到并运行了错误。py:14:UserWarning:gzip传输编码是实验性的!br.set_handle_gzip(True)Traceback(上次调用):文件“run.py”,第24行,在proxytype、proxyserv、proxyport=line.split(“:”)#提取代理类型、ip和端口值错误:需要2个以上的值才能解包您需要将代理类型添加到
proxylist.txt
文件中。每一行必须具有格式
协议:ip:port
,如下所示:
http:111.68.103.39:3128
,或
ftp:83.246.226.42:8080
。按您所说添加后,我得到另一个名为run.py:14:UserWarning:gzip传输编码的错误是实验性的!br.set_handle_gzip(True)检索URL。。。回溯(最近一次调用):文件“run.py”,第32行,html=response.read()name错误:名称“response”未定义我不会为您修复所有代码。因为我没有mechanize的经验,而且这不是堆栈溢出的原因。我已经回答了你原来的问题,剩下的就看你了。感谢你所做的任何帮助(Y)可以在使用urllib时创建这样一个,如果你能粘贴下面的代码,谢谢:)