【问题标题】:urllib.error.HTTPError: HTTP Error 403: Forbidden Pythonurllib.error.HTTPError:HTTP 错误 403:禁止 Python
【发布时间】:2016-09-15 15:36:05
【问题描述】:

我的代码:

import sqlite3, os, urllib.request
from xml.dom import minidom

if os.path.exists("data.db"):
  con = sqlite3.connect("data.db")
  cursor = con.cursor()
  sql = "SELECT * FROM data WHERE test= '123'"
  cursor.execute(sql)
else:
  print("ERROR")

for dsatz in cursor:
  #print(dsatz)
  link = 'http://test.org/publication/' + dsatz[0] + '' + dsatz[1] +'/bib'
  #print(link)

  web_data = urllib.request.urlopen(link)
  xmldoc = minidom.parse(web_data)

  di = xmldoc.getElementsByTagName("document-id")[:1]

  for x in di:
      publicationcountry = x.getElementsByTagName("country")[0].firstChild.data
      publicationdocnumber = x.getElementsByTagName("doc-number")[0].firstChild.data
      punlicationkind = x.getElementsByTagName("kind")[0].firstChild.data
      publicationdate = x.getElementsByTagName("date")[0].firstChild.data     

      sql = "INSERT INTO link_xml_data VALUES('" \
      + publicationcountry + "', '" \
      + str(publicationdocnumber) + "', '" \
      + punlicationkind + "')"

      con.close()

但是在 15 个链接之后我得到了错误:

Traceback (most recent call last):
  File "C:\Users\j\3.py", line 34, in <module>
    web_data = urllib.request.urlopen(link)
  File "C:\Users\j\Python35-32\lib\urllib\request.py", line 163, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\j\Python35-32\lib\urllib\request.py", line 472, in open
    response = meth(req, response)
  File "C:\Users\j\Python35-32\lib\urllib\request.py", line 582, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Users\j\Python35-32\lib\urllib\request.py", line 510, in error
    return self._call_chain(*args)
  File "C:\Users\j\Python35-32\lib\urllib\request.py", line 444, in _call_chain
    result = func(*args)
  File "C:\Users\j\Python35-32\lib\urllib\request.py", line 590, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

我应该添加或更改什么?

【问题讨论】:

    标签: python xml parsing


    【解决方案1】:

    网络服务器告诉您链接被禁止。您的代码(可能)没有任何问题。

    是否某些链接总是有效而其他链接总是失败,或者模式是否会随着时间而改变?

    在收到 403 Forbidden 响应后,您是否尝试返回并重新请求之前成功的链接之一?

    也许服务器最终将您识别为网络爬虫并告诉您离开?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-22
      • 1970-01-01
      • 1970-01-01
      • 2014-05-17
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      相关资源
      最近更新 更多