【问题标题】:Web scraping using python: urlopen returns HTTP Error 403: Forbidden使用 python 抓取网页:urlopen 返回 HTTP 错误 403:禁止
【发布时间】:2020-07-07 17:25:43
【问题描述】:

我正在尝试使用 urlopen 从 Fragantica.com 下载数据,但即使在更改用户代理并添加标头之后也会出现错误(“HTTP 错误 403:禁止”)。 我也尝试过这里的代码,但没有成功 (http://wolfprojects.altervista.org/changeua.php#problem)。

这是我的代码:

import urllib.request

user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.4 Safari/605.1.15'



url = "https://www.fragrantica.com/perfume/Tom-Ford/Tobacco-Vanille-1825.html"
headers={'User-Agent':user_agent,} 

request=urllib.request.Request(url,None,headers) #The assembled request
response = urllib.request.urlopen(request)
data = response.read() # The data u need

这是我遇到的错误:HTTPError: HTTP Error 403: Forbidden

【问题讨论】:

    标签: python python-requests urlopen


    【解决方案1】:

    您可能需要指定更多标题,试试这个:

    import urllib.request    
    
    url = "https://www.fragrantica.com/perfume/Tom-Ford/Tobacco-Vanille-1825.html"
    headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.4 Safari/605.1.15',
           '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'} 
    
    request=urllib.request.Request(url=url, headers=headers) #The assembled request
    response = urllib.request.urlopen(request)
    data = response.read() # The data u need
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-15
      • 2018-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-20
      • 1970-01-01
      • 2020-04-01
      相关资源
      最近更新 更多