zoukankan      html  css  js  c++  java
  • urllib2.HTTPError: HTTP Error 403: Forbidden

    这个问题主要是没有headers,加入一些内容就可以了

    示例:

    # -*- coding: UTF-8 -*-
    import urllib2
    
    site= "http://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/getHistoricalData.jsp?symbol=JPASSOCIAT&fromDate=1-JAN-2012&toDate=1-AUG-2012&datePeriod=unselected&hiddDwnld=true"
    
    hdr = {'User-Agent':'Mozilla/5.0'}
    
    req = urllib2.Request(site,headers=hdr)
    
    page = urllib2.urlopen(req)

    出现标题中错误:

    加入headers:

     1 import urllib2
     2 
     3 site= "http://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/getHistoricalData.jsp?symbol=JPASSOCIAT&fromDate=1-JAN-2012&toDate=1-AUG-2012&datePeriod=unselected&hiddDwnld=true"
     4 hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
     5        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
     6        'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
     7        'Accept-Encoding': 'none',
     8        'Accept-Language': 'en-US,en;q=0.8',
     9        'Connection': 'keep-alive'}
    10 
    11 req = urllib2.Request(site, headers=hdr)
    12 
    13 try:
    14     page = urllib2.urlopen(req)
    15 except urllib2.HTTPError, e:
    16     print e.fp.read()
    17 
    18 content = page.read()
    19 print content

    看看是不是成功了?!

    点滴积累,点滴生活,丰富研究生生活!
  • 相关阅读:
    【CH6801】棋盘覆盖
    【模板】二分图匹配
    【POJ3683】Priest John's Busiest Day
    【Asia Yokohama Regional Contest 2018】Arithmetic Progressions
    【POJ2230】Watchcow
    【CF263D】Cycle in Graph
    【CF131D】Subway
    【洛谷P2709】小B的询问
    【模板】2-SAT
    【模板】矩阵快速幂
  • 原文地址:https://www.cnblogs.com/nju2014/p/4456845.html
Copyright © 2011-2022 走看看