Pycharm- HTTPError:HTTP错误403:禁止使用urllib2获取数据

时间:2017-09-07 17:07:01

标签: python web-scraping urllib2

我尝试从网址中获取数据,但我发现该网址在我的国家/地区受到了谴责。所以我启用代理和配置Pycharm来使用它。在测试连接时,连接到该URL是成功的,但在运行代码时仍然是

我收到此错误:HTTPError:HTTP错误403:禁止

我在网上传阅了答案,我得知如下:

hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
   'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
   'Accept-Encoding': 'none',
   'Accept-Language': 'en-US,en;q=0.8',
   'Connection': 'keep-alive'}

req = urllib2.Request(site, headers=hdr)

但它没用!

我的代码如下:

import urllib2 from bs4 import BeautifulSoup

def getAllDoxyPost(url, links):

request = urllib2.Request(url)
response = urllib2.urlopen(request)
soup = BeautifulSoup(response)

for a in soup.find_all('a'):
    try:
        url = a['href']
        title = a['title']
        if title == 'Older Posts':
            print title, url
            links.append(url)
            getAllDoxyPost(url, links)
    except:
        title = " "
return 

blogUrl = "http://doxydonkey.blogspot.in"
links = []
getAllDoxyPost(blogUrl, links)

0 个答案:

没有答案