我正在尝试修复以下错误。但是我没有找到任何解决方案。谁能帮我这个?当我运行此代码时,有时会运行该代码,但有时会显示以下错误。以下是带有错误的代码
import requests
from bs4 import BeautifulSoup
import mysql.connector
mydb = mysql.connector.connect(host="localhost", user="root",passwd="", database="python_db")
mycursor = mydb.cursor()
#url="https://csr.gov.in/companyprofile.php?year=FY%202014-15&CIN=U01224KA1980PLC003802"
#query1 = "INSERT INTO csr_details(average_net_profit,csr_prescribed_expenditure,csr_spent,local_area_spent) VALUES()"
mycursor.execute("SELECT cin_no FROM tn_cin WHERE csr_status=0")
urls=mycursor.fetchall()
#print(urls)
def convertTuple(tup):
str = ''.join(tup)
return str
for url in urls:
str = convertTuple(url[0])
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36', "Accept-Language": "en-US,en;q=0.9", "Accept-Encoding": "gzip, deflate"}
csr_link = 'https://csr.gov.in/companyprofile.php?year=FY%202014-15&CIN='
link = csr_link+str
#print(link)
response=requests.get(link, headers=headers)
#print(response.status_code)
bs=BeautifulSoup(response.text,"html.parser")
div_table=bs.find('div', id = 'colfy4')
if div_table is not None:
fy_table = div_table.find_all('table', id = 'employee_data')
if fy_table is not None:
for tr in fy_table:
td=tr.find_all('td')
if len(td)>0:
rows=[i.text for i in td]
row1=rows[0]
row2=rows[1]
row3=rows[2]
row4=rows[3]
#cin_no=url[1]
#cin=convertTuple(url[1])
#result=cin_no+rows
mycursor.execute("INSERT INTO csr_details(cin_no,average_net_profit,csr_prescribed_expenditure,csr_spent,local_area_spent) VALUES(%s,%s,%s,%s,%s)",(str,row1,row2,row3,row4))
#print(cin)
#print(str)
#var=1
status_update="UPDATE tn_cin SET csr_status=%s WHERE cin_no=%s"
data = ('1',str)
mycursor.execute(status_update,data)
#result=mycursor.fetchall()
#print(result)
mydb.commit()
运行上述代码后出现以下错误
requests.exceptions.ConnectionError:('连接被中止。',RemoteDisconnected('远端封闭的无响应的连接'))
错误
requests.exceptions.ConnectionError:('连接被中止。',RemoteDisconnected('远端封闭的无响应的连接'))
通常是在服务器端引起的错误,该错误通常归为5xx
状态码。该错误仅表明在一个实例中,服务器在交付完整响应之前将其关闭。
我相信可能是此行引起的
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36', "Accept-Language": "en-US,en;q=0.9", "Accept-Encoding": "gzip, deflate"}
在某些情况下,header
值有问题。您可以简单地尝试将标题设置为
response=requests.get(link, headers={"User-Agent":"Mozilla/5.0"})
看看是否能解决您的问题。
See this answer用于各种浏览器的用户代理。