No connection adapters were found for Python3-Requests
我正在使用python3中的请求包进行网络报废。 这是我的代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import csv from datetime import datetime import requests import csv from datetime import datetime from bs4 import BeautifulSoup quote_page = ['http://10.69.161.179:8080']; data = [] page = requests.get(quote_page) soup = BeautifulSoup(page.content,'html.parser') name_box = soup.find('div', attrs={'class':'caption span10'}) name= name_box.text.strip() #strip() is used to remove starting and ending print(name); data.append(name) with open('sample.csv', 'a') as csv_file: writer = csv.writer(csv_file) writer.writerow([name]) print ("Success"); |
当我执行上面的代码时,我收到以下错误。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | Traceback (most recent call last): File"first_try.py", line 21, in <module> page = requests.get(quote_page); File"C:\Python\lib\site-packages equests-2.13.0-py3.6.egg equests\api.py", line 70, in get return request('get', url, params=params, **kwargs) File"C:\Python\lib\site-packages equests-2.13.0-py3.6.egg equests\api.py", line 56, in request return session.request(method=method, url=url, **kwargs) File"C:\Python\lib\site-packages equests-2.13.0-py3.6.egg equests\sessions.py", line 488, in request resp = self.send(prep, **send_kwargs) File"C:\Python\lib\site-packages equests-2.13.0-py3.6.egg equests\sessions.py", line 603, in send adapter = self.get_adapter(url=request.url) File"C:\Python\lib\site-packages equests-2.13.0-py3.6.egg equests\sessions.py", line 685, in get_adapter raise InvalidSchema("No connection adapters were found for '%s'" % url) requests.exceptions.InvalidSchema: No connection adapters were found for '['http://10.69.161.179:8080/#/main/dashboard/metrics']' |
谁能帮我这个?:(
因为requests.get()只接受字符串格式的url架构。 你需要在列表[]中解压缩字符串。
1 2 3 4 | quote_page = ['http://10.69.161.179:8080'] for url in quote_page: page = requests.get(url) ..... |
顺便说一下,虽然分号在下面的陈述中是无害的,但你应该避免它,除非你出于某种原因需要它
1 | quote_page = ['http://10.69.161.179:8080']; |