This code works ok for a few minutes then stops with a timeout error
Here's the code:
Here's the code working fine....
Weight loss jab 'could reduce heart attack risk'
Watch: Joe Lycett dressed as Queen Elizabeth I
Man who murdered family has jail sentence reduced
And here's the timeout errorI have tried adjusting the timeout figure and the sleep period, but it makes no difference.
What can I do to keep the code running for hours rather than minutes?
Thank you.
Here's the code:
Code:
from time import time, sleepfrom bs4 import BeautifulSoupimport requestsfrom random import randrangewhile True: # Get news headline from an RSS Feed rnd = (randrange(25)) url = requests.get('https://feeds.bbci.co.uk/news/uk/rss.xml', timeout = 22 ,verify=True) soup = BeautifulSoup(url.content, 'xml') items = soup.find_all('item') res = [items[rnd]] # Get a random item sleep(5) for i in res: news = i.title.text print(news)
Here's the code working fine....
Weight loss jab 'could reduce heart attack risk'
Watch: Joe Lycett dressed as Queen Elizabeth I
Man who murdered family has jail sentence reduced
And here's the timeout error
Code:
Traceback (most recent call last): File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 162, in _new_conn (self._dns_host, self.port), self.timeout, **extra_kw) File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 80, in create_connection raise err File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 70, in create_connection sock.connect(sa)TimeoutError: [Errno 110] Connection timed outDuring handling of the above exception, another exception occurred:Traceback (most recent call last): File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 600, in urlopen chunked=chunked) File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 343, in _make_request self._validate_conn(conn) File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 841, in _validate_conn conn.connect() File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 315, in connect conn = self._new_conn() File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 171, in _new_conn self, "Failed to establish a new connection: %s" % e)urllib3.exceptions.NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x735c1c10>: Failed to establish a new connection: [Errno 110] Connection timed outDuring handling of the above exception, another exception occurred:Traceback (most recent call last): File "/usr/local/lib/python3.7/dist-packages/requests/adapters.py", line 496, in send chunked=chunked, File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 638, in urlopen _stacktrace=sys.exc_info()[2]) File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 399, in increment raise MaxRetryError(_pool, url, error or ResponseError(cause))urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='feeds.bbci.co.uk', port=443): Max retries exceeded with url: /news/uk/rss.xml (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x735c1c10>: Failed to establish a new connection: [Errno 110] Connection timed out'))
What can I do to keep the code running for hours rather than minutes?
Thank you.
Statistics: Posted by richard238 — Tue May 14, 2024 2:58 pm