我们的网络会两个小时断一次,需要手动在网页上登录一下。于是我想自己做个登录器代码如下
import time
import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
options = Options()
options.headless = True
driver = webdriver.Chrome(options=options, executable_path=r'D:\Program Files\chromedriver.exe')
url = 'https://onboardicafe.com/royal/defaultlogin.aspx'
while True:
r = requests.get(url)
html = r.text
bsoup = BeautifulSoup(html, "lxml")
information = bsoup.find('span', id='MainContent_LabelLoginSuccessStats')
if information == None:
driver.get(url)
driver.implicitly_wait(3)
username = 'username'
password = '1234'
driver.find_element_by_id("login_username").send_keys(username)
driver.find_element_by_id("login_pin").send_keys(password)
driver.find_element_by_id("login_btn").click()
time.sleep(3)
driver.find_element_by_id("ButtonPanelPackagesCustomPurchase").click()
driver.quit()
time.sleep(3)
else:
hours = str(information).split(',')[0].replace('<span id="MainContent_LabelLoginSuccessStats">','').replace(" ",'').replace('Hours','').replace('Hour','')
minutes = str(information).split(",")[1].replace('of Wi-Fi remaining</span>','').replace(' ','').replace('Minutes','').replace('Minute','')
time_left = hours + "小时" + minutes + "分"
print('剩余连接时间为'+ time_left)
sleeptime = (float(hours) * 60 + float(minutes)) * 60
time.sleep(sleeptime)
登录前和登录后的网站是一样的,于是我requests到网页数据给bs4解析,如果显示有剩余时间,那就代表还没结束,用time强制等待
如果找不到显示时间,就代表跳转到了登录界面,那就用selenium后台登录
这样大概能正常运行1-2次,在第二次或者第三次就会报错,错误代码如下:
Traceback (most recent call last):
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 159, in _new_conn
conn = connection.create_connection(
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\connection.py", line 84, in create_connection
raise err
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\connection.py", line 74, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法连接。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 670, in urlopen
httplib_response = self._make_request(
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 392, in _make_request
conn.request(method, url, **httplib_request_kw)
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1230, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1276, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1225, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1004, in _send_output
self.send(msg)
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 944, in send
self.connect()
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 187, in connect
conn = self._new_conn()
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 171, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x000001C8D58BB8E0>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/Project/Music/Main.py", line 18, in <module>
driver.get(url)
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 333, in get
self.execute(Command.GET, {'url': url})
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 319, in execute
response = self.command_executor.execute(driver_command, params)
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 374, in execute
return self._request(command_info[0], url, body=data)
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 397, in _request
resp = self._conn.request(method, url, body=body, headers=headers)
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\request.py", line 79, in request
return self.request_encode_body(
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\request.py", line 171, in request_encode_body
return self.urlopen(method, url, **extra_kw)
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\poolmanager.py", line 336, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 752, in urlopen
return self.urlopen(
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 752, in urlopen
return self.urlopen(
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 752, in urlopen
return self.urlopen(
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 724, in urlopen
retries = retries.increment(
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\retry.py", line 439, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=54337): Max retries exceeded with url: /session/391a2a3e3c3508a0e110d59a3f97e2d4/url (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001C8D58BB8E0>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。'))
想问问是我的代码问题,还是计算机的问题。。。
虽然我觉得应该是我的问题,但是不知道出在哪里。。。
import time
import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
options = Options()
options.headless = True
driver = webdriver.Chrome(options=options, executable_path=r'D:\Program Files\chromedriver.exe')
url = 'https://onboardicafe.com/royal/defaultlogin.aspx'
while True:
r = requests.get(url)
html = r.text
bsoup = BeautifulSoup(html, "lxml")
information = bsoup.find('span', id='MainContent_LabelLoginSuccessStats')
if information == None:
driver.get(url)
driver.implicitly_wait(3)
username = 'username'
password = '1234'
driver.find_element_by_id("login_username").send_keys(username)
driver.find_element_by_id("login_pin").send_keys(password)
driver.find_element_by_id("login_btn").click()
time.sleep(3)
driver.find_element_by_id("ButtonPanelPackagesCustomPurchase").click()
driver.quit()
time.sleep(3)
else:
hours = str(information).split(',')[0].replace('<span id="MainContent_LabelLoginSuccessStats">','').replace(" ",'').replace('Hours','').replace('Hour','')
minutes = str(information).split(",")[1].replace('of Wi-Fi remaining</span>','').replace(' ','').replace('Minutes','').replace('Minute','')
time_left = hours + "小时" + minutes + "分"
print('剩余连接时间为'+ time_left)
sleeptime = (float(hours) * 60 + float(minutes)) * 60
time.sleep(sleeptime)
登录前和登录后的网站是一样的,于是我requests到网页数据给bs4解析,如果显示有剩余时间,那就代表还没结束,用time强制等待
如果找不到显示时间,就代表跳转到了登录界面,那就用selenium后台登录
这样大概能正常运行1-2次,在第二次或者第三次就会报错,错误代码如下:
Traceback (most recent call last):
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 159, in _new_conn
conn = connection.create_connection(
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\connection.py", line 84, in create_connection
raise err
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\connection.py", line 74, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法连接。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 670, in urlopen
httplib_response = self._make_request(
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 392, in _make_request
conn.request(method, url, **httplib_request_kw)
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1230, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1276, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1225, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1004, in _send_output
self.send(msg)
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 944, in send
self.connect()
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 187, in connect
conn = self._new_conn()
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 171, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x000001C8D58BB8E0>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/Project/Music/Main.py", line 18, in <module>
driver.get(url)
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 333, in get
self.execute(Command.GET, {'url': url})
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 319, in execute
response = self.command_executor.execute(driver_command, params)
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 374, in execute
return self._request(command_info[0], url, body=data)
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 397, in _request
resp = self._conn.request(method, url, body=body, headers=headers)
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\request.py", line 79, in request
return self.request_encode_body(
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\request.py", line 171, in request_encode_body
return self.urlopen(method, url, **extra_kw)
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\poolmanager.py", line 336, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 752, in urlopen
return self.urlopen(
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 752, in urlopen
return self.urlopen(
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 752, in urlopen
return self.urlopen(
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 724, in urlopen
retries = retries.increment(
File "C:\Users\GeneYang\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\retry.py", line 439, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=54337): Max retries exceeded with url: /session/391a2a3e3c3508a0e110d59a3f97e2d4/url (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001C8D58BB8E0>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。'))
想问问是我的代码问题,还是计算机的问题。。。
虽然我觉得应该是我的问题,但是不知道出在哪里。。。