我正在使用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");
当我执行上面的代码时,我收到以下错误。
追踪(最近一次通话): 文件“first_try.py”,第21行,in page = requests.get(quote_page); 文件“C:\ Python \ lib \ site-packages \ requests-2.13.0-py3.6.egg \ requests \ api.py”,第70行,在get中 返回请求('get',url,params = params,** kwargs) 请求中的文件“C:\ Python \ lib \ site-packages \ requests-2.13.0-py3.6.egg \ requests \ api.py”,第56行 return session.request(method = method,url = url,** kwargs) 请求中的文件“C:\ Python \ lib \ site-packages \ requests-2.13.0-py3.6.egg \ requests \ sessions.py”,第488行 resp = self.send(prep,** send_kwargs) 文件“C:\ Python \ lib \ site-packages \ requests-2.13.0-py3.6.egg \ requests \ sessions.py”,第603行,发送 adapter = self.get_adapter(url = request.url) get_adapter中的文件“C:\ Python \ lib \ site-packages \ requests-2.13.0-py3.6.egg \ requests \ sessions.py”,第685行 提出InvalidSchema(“找不到'%s'”%url的连接适配器) requests.exceptions.InvalidSchema:找不到'['http://10.69.161.179:8080/#/main/dashboard/metrics']'
的连接适配器任何人都可以帮我吗? :(
答案 0 :(得分:2)
因为requests.get()只接受字符串格式的url架构。你需要在列表[]中解压缩字符串。
quote_page = ['http://10.69.161.179:8080']
for url in quote_page:
page = requests.get(url)
.....
顺便说一下,虽然在下面的陈述中分号是无害的,但除非you need it for some reason
,否则你应该避免使用分号quote_page = ['http://10.69.161.179:8080'];