HTTP错误403:使用urllib.request时禁止

时间:2017-08-27 06:10:22

标签: python python-3.x urllib

我尝试运行此代码,但始终收到错误HTTP Error 403: Forbidden

import urllib.request

try:
    url = urllib.request.urlopen('http://google.com/search?q=test')
    headers = {}
    headers['User-Agent'] ='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36' 
    req = urllib.request.Request(url, headers=headers)
    resp = urllib.request.urlopen(req)
    resp_Data = resp.read()

    savefile = open('newFile.txt', 'w')
    savefile.write(str(resp_Data))
    savefile.close()
except Exception as e:
    print(str(e))

有没有人可以帮我这个代码,因为我在这里找不到解决方案?

2 个答案:

答案 0 :(得分:1)

更改此行

url = urllib.request.urlopen('http://google.com/search?q=test')

url = 'http://google.com/search?q=test'

答案 1 :(得分:0)

看起来你打开了两次网址。这里有两条建议:

  1. 为什么不直接使用requests模块?首先,安装它:

    pip install requests
    
  2. 使用with..as上下文管理器来处理文件I / O

  3. import requests
    
    headers =  {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36' }    
    resp_data = requests.get('http://google.com/search?q=test', headers=headers).text
    
    with open('newFile.txt', 'w') as savefile:
        savefile.write(resp_data)