我正在尝试制作一个基本的网络抓取工具。我的互联网是通过代理连接。所以我使用了here给出的解决方案。但仍然在运行代码时我收到了错误。 我的代码是:
#!/usr/bin/python3.4
import requests
from bs4 import BeautifulSoup
import urllib.request as req
proxies = {
"http": r"http://usr:pass@202.141.80.22:3128",
"https": r"http://usr:pass@202.141.80.22:3128",
}
url = input("Ask user for something")
def santabanta(max_pages,url):
page = 1
while (page <= max_pages):
source_code = requests.get(url,proxies=proxies)
plain_text = source_code.text
print (plain_text)
soup = BeautifulSoup(plain_text,"lxml")
for link in soup.findAll('a'):
href = link.get('href')
print(href)
page = page + 1
santabanta(1,url)
但是在ubuntu 14.04中运行终端时,我收到以下错误:
尝试检索网址时遇到以下错误: http://www.santabanta.com/wallpapers/gauhar-khan/?
缓存访问被拒绝。
抱歉,目前不允许您申请http://www.santabanta.com/wallpapers/gauhar-khan/?从此缓存开始直到您自己进行身份验证。
我发布的网址是:http://www.santabanta.com/wallpapers/gauhar-khan/
请帮帮我
答案 0 :(得分:1)
。[这是一张图片来帮助你] [1] [1]:http://i.stack.imgur.com/zUEBE.png
标题如下:
headers = {'接受':'* / *', 'Accept-Encoding':'gzip,deflate,sdch', '接受 - 语言': '的en-US,连接; Q = 0.8', “缓存控制”:“最大年龄= 0”, “连接”:“保活”, '代理授权':'基本ZWRjZ3Vlc3Q6ZWRjZ3Vlc3Q =', 'If-Modified-Since':'周五,2015年11月13日17:47:23 GMT', 'User-Agent':'Mozilla / 5.0(X11; Linux x86_64)AppleWebKit / 537.36(KHTML,与Gecko一样)Chrome / 48.0.2564.116 Safari / 537.36' }
答案 1 :(得分:0)
还有另一种方法可以解决这个问题 你可以做的是让你的python脚本使用你的环境变量中定义的代理
打开终端(CTRL + ALT + T)
export http_proxy="http://usr:pass@proxy:port"
export https_proxy="https://usr:pass@proxy:port"
并从代码中删除代理行
这是更改后的代码:
#!/usr/bin/python3.4
import requests
from bs4 import BeautifulSoup
import urllib.request as req
url = input("Ask user for something")
def santabanta(max_pages,url):
page = 1
while (page <= max_pages):
source_code = requests.get(url)
plain_text = source_code.text
print (plain_text)
soup = BeautifulSoup(plain_text,"lxml")
for link in soup.findAll('a'):
href = link.get('href')
print(href)
page = page + 1
santabanta(1,url)