HttpUrlLib.错误:HTTP错误403:禁止的Python

2024-06-17 12:37:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码:

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个链接之后,我得到了一个错误:

^{pr2}$

我应该添加或更改什么?在


Tags: importsqldataosrequestlinkxmlurllib
1条回答
网友
1楼 · 发布于 2024-06-17 12:37:00

网络服务器告诉你链接是禁止的。你的代码(可能)没什么问题。在

是某些链接始终有效而其他链接总是失败,还是模式会随着时间而改变?在

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

也许服务器最终会认出你是一个网络刮板,并告诉你离开?在

相关问题 更多 >