我正在编写一个小程序,通过提供一个URL来从网页中获取所有超链接,但它看起来像我所使用的网络使用代理,并且无法获取。 。 我的代码:如何通过代理使用Python访问网页
import sys
import urllib
import urlparse
from bs4 import BeautifulSoup
def process(url):
page = urllib.urlopen(url)
text = page.read()
page.close()
soup = BeautifulSoup(text)
with open('s.txt','w') as file:
for tag in soup.findAll('a', href=True):
tag['href'] = urlparse.urljoin(url, tag['href'])
print tag['href']
file.write('\n')
file.write(tag['href'])
def main():
if len(sys.argv) == 1:
print 'No url !!'
sys.exit(1)
for url in sys.argv[1:]:
process(url)
根据您的问题,您的网络可能使用或不使用代理。你可以更具体一些,或只是通过管理员并询问? – frlan
是的,它有一个代理,我试着在家里它工作正常,但当我把它给我的部门,以显示给我的老师,它的力量工作...这是错误 'IOError:[Errno套接字错误] [Errno -2]名称或服务未知' –
这是我使用的代理连接“proxy4.nehu.ac.in:3128”我如何将它放在我的程序中的代码..?请帮助,我很困扰它。 –