Vic*_*cky 0 python-3.x python-requests
我在 python3 中使用带有请求包的漂亮汤来进行网页抓取。这是我的代码。
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");
Run Code Online (Sandbox Code Playgroud)
当我执行上述代码时,出现以下错误。
Traceback (most recent call last):
File "first_try.py", line 21, in <module>
page = requests.get(quote_page);
File "C:\Python\lib\site-packages\requests-2.13.0-py3.6.egg\requests\api.py", line 70, in get
return request('get', url, params=params, **kwargs)
File "C:\Python\lib\site-packages\requests-2.13.0-py3.6.egg\requests\api.py", line 56, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Python\lib\site-packages\requests-2.13.0-py3.6.egg\requests\sessions.py", line 488, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python\lib\site-packages\requests-2.13.0-py3.6.egg\requests\sessions.py", line 603, in send
adapter = self.get_adapter(url=request.url)
File "C:\Python\lib\site-packages\requests-2.13.0-py3.6.egg\requests\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']'
Run Code Online (Sandbox Code Playgroud)
谁能帮我这个?:(
因为 requests.get() 只接受字符串格式的 url 模式。您需要在列表 [] 中解压字符串。
quote_page = ['http://10.69.161.179:8080']
for url in quote_page:
page = requests.get(url)
.....
Run Code Online (Sandbox Code Playgroud)
顺便说一句,虽然分号在以下语句中是无害的,但除非出于某种原因需要它,否则您应该避免使用它
quote_page = ['http://10.69.161.179:8080'];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15626 次 |
| 最近记录: |