urllib2.HTTPError: HTTP Error 403: Forbidden

后端 未结 3 1921
无人及你
无人及你 2020-11-22 10:44

I am trying to automate download of historic stock data using python. The URL I am trying to open responds with a CSV file, but I am unable to open using urllib2. I have tri

3条回答
  •  忘掉有多难
    2020-11-22 11:10

    This will work in Python 3

    import urllib.request
    
    user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
    
    url = "http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers"
    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
    

提交回复
热议问题