昨天在写一个python小说爬虫的时候,按照平常来说啥毛病都没有,废话就不说了先上菜:

源码信息:

import time
import requests
from bs4 import BeautifulSoup
import pymysql

print('连接到了mysql服务器')
conn = pymysql.connect(host='admin30209.mysql.rds.aliyuncs.com',
                       port=20032,
                       user='root',
                       pad='',
                       db='book')
print('连接上了')
cursor = conn.cursor()
# 获取当地的时间,并转换成固定形式
now = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
table_name = "笔趣阁更新榜" + repr(now)  # 加上``有这个'""'
print(table_name)

#  创建数据表的sql 语句  并设置name_id 为主键自增长不为空
sql_createTb = """CREATE TABLE  `%s`(
                 书名 CHAR(25) NOT NULL,
                 地址 CHAR(225))
                 """ % (table_name)
# % pymysql.escape_string(table_name)有\\
cursor.execute(sql_createTb)
# 往表里面插入数据
hdrs = {'User-Agent': 'Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)'}


url = 'http://www.biquger.net/'

r = requests.get(url, headers=hdrs)

time.sleep(6)

soup = BeautifulSoup(r.content.decode('gbk', 'ignore'), 'lxml')
lis = soup.find_all('a', 'poptext')

for tr in lis:
    href = tr.get('href')
    print(href)
    name = tr.get_text()
    print(name)

    sql = "INSERT INTO `%s`" % table_name + " VALUES(%s, %s)"
    data_color = (name, href)
    cursor.execute(sql, data_color)
    conn.commit()
    print('ok')
print('榜单更新完成')

具体报错信息: 

Traceback (most recent call last):
  File "C:\Users\aimo\PycharmProjects\General contents\venv\lib\site-packages\urllib3\connectionpool.py", line 710, in urlopen
    chunked=chunked,
  File "C:\Users\aimo\PycharmProjects\General contents\venv\lib\site-packages\urllib3\connectionpool.py", line 449, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "C:\Users\aimo\PycharmProjects\General contents\venv\lib\site-packages\urllib3\connectionpool.py", line 444, in _make_request
    httplib_response = conn.getresponse()
  File "C:\Users\aimo\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1331, in getresponse
    response.begin()
  File "C:\Users\aimo\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "C:\Users\aimo\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "C:\Users\aimo\AppData\Local\Programs\Python\Python36\lib\socket.py", line 586, in readinto
    return self._sock.recv_into(b)
ConnectionAbortedError: [WinError 10053] 你的主机中的软件中止了一个已建立的连接。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\aimo\PycharmProjects\General contents\venv\lib\site-packages\requests\adapters.py", line 450, in send
    timeout=timeout
  File "C:\Users\aimo\PycharmProjects\General contents\venv\lib\site-packages\urllib3\connectionpool.py", line 788, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "C:\Users\aimo\PycharmProjects\General contents\venv\lib\site-packages\urllib3\util\retry.py", line 550, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "C:\Users\aimo\PycharmProjects\General contents\venv\lib\site-packages\urllib3\packages\six.py", line 769, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\aimo\PycharmProjects\General contents\venv\lib\site-packages\urllib3\connectionpool.py", line 710, in urlopen
    chunked=chunked,
  File "C:\Users\aimo\PycharmProjects\General contents\venv\lib\site-packages\urllib3\connectionpool.py", line 449, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "C:\Users\aimo\PycharmProjects\General contents\venv\lib\site-packages\urllib3\connectionpool.py", line 444, in _make_request
    httplib_response = conn.getresponse()
  File "C:\Users\aimo\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1331, in getresponse
    response.begin()
  File "C:\Users\aimo\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "C:\Users\aimo\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "C:\Users\aimo\AppData\Local\Programs\Python\Python36\lib\socket.py", line 586, in readinto
    return self._sock.recv_into(b)
urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionAbortedError(10053, '你的主机中的软件中止了一个已建立的连接。', None, 10053, None))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/aimo/PycharmProjects/General contents/test/xp_test.py", line 5, in <module>
    r = requests.get(url)
  File "C:\Users\aimo\PycharmProjects\General contents\venv\lib\site-packages\requests\api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Users\aimo\PycharmProjects\General contents\venv\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\aimo\PycharmProjects\General contents\venv\lib\site-packages\requests\sessions.py", line 529, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\aimo\PycharmProjects\General contents\venv\lib\site-packages\requests\sessions.py", line 645, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\aimo\PycharmProjects\General contents\venv\lib\site-packages\requests\adapters.py", line 501, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionAbortedError(10053, '你的主机中的软件中止了一个已建立的连接。', None, 10053, None))

报错信息

requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionAbortedError(10053, '你的主机中的软件中止了一个已建立的连接。', None, 10053, None))

我第一次运行的时候可以完美运行, 第二次运行就开始给我报错。

经过九九八十一难的百度,谷歌,搜索,最后的结果就是这些都没有没有给我找到解决的办法,话不多说,我的结果是:因该是你之前运行的程序进程还没有结束掉,你再运行后肯定就无法连接。解决办法就是等他结束了进程,再去运行。QWQ

纯属个人理解, 希望遇到同样问题的可以借鉴一下, 同时如果有大神看到的或者知道的读者,希望赐教一下具体的原因, 解解疑惑。
 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐