【问题标题】:How to solve requests.exceptions.ConnectionError: ('Connection aborted.') in python web scraping?python web抓取中如何解决requests.exceptions.ConnectionError: ('Connection aborted.')?
【发布时间】:2020-06-10 15:54:28
【问题描述】:

我正在尝试修复以下错误。但我没有找到任何解决方案。谁能帮我这个? 当我运行此代码时,有时它会运行代码,但有时会显示以下错误。以下是错误代码

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: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

【问题讨论】:

    标签: python web-scraping beautifulsoup python-requests


    【解决方案1】:

    错误

    requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

    通常是在服务器端引起的错误,该错误通常归类为状态码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 用于各种浏览器的用户代理。

    【讨论】:

    • 感谢您的帮助。但这并没有解决错误。仍然显示相同的错误
    • 您提到它有时会起作用 - 您是否能够注意到运行环境中的任何不同之处?您可能还想逐行调试代码以找到确切的点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-02
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 2023-01-10
    • 1970-01-01
    相关资源
    最近更新 更多