【发布时间】:2015-09-22 08:50:42
【问题描述】:
我正在编写一个小程序,通过提供 URL 从网页中获取所有超链接,但我所在的网络似乎正在使用代理,并且无法获取.. 我的代码:
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)
【问题讨论】:
-
根据您的问题,您的网络可能有也可能没有正在使用的代理。你能更具体一点,或者只是通过你的管理员询问?
-
是的,它有一个代理,我在家里试过它工作正常,但是当我把它带到我的部门向我的老师展示它的工作时......这是错误
IOError: [Errno socket error] [Errno -2] Name or service not known -
这也是我使用的代理连接“proxy4.nehu.ac.in:3128”我如何将它放入我的程序中的代码中......?请帮忙,我很坚持。
-
好的,我会检查一下,如果我遇到一些问题,我会回复你..此时我无法测试它,因为我必须在大学本身尝试,因为我没有代理网络测试。如果你没问题?
-
您可以轻松地自行设置代理。例如。鱿鱼很受欢迎。
标签: python beautifulsoup urllib