import requests
import csv
import requests
from bs4 import BeautifulSoup
r = requests.get('https://pqt.cbp.gov/report/YYZ_1/12-01-2017')
soup = BeautifulSoup(r)
table = soup.find('table', attrs={ "class" : "table-horizontal-line"})
headers = [header.text for header in table.find_all('th')]
rows = []
for row in table.find_all('tr'):
rows.append([val.text.encode('utf8') for val in row.find_all('td')])
with open('output_file.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerow(headers)
writer.writerows(row for row in rows if row)
我正在尝试解析这个特定网页中的所有表数据:https://pqt.cbp.gov/report/YYZ_1/12-01-2017
我在soup = BeautifulSoup(r)
行中得到一个错误。我得到一个错误TypeError: object of type 'Response' has no len()
。我也不确定我的逻辑是否正确。请帮我翻译一下表格数据
尝试:
变量
r
是类型Response
不是str
,使用r.text
或r.content
并且没有类table-horizontal-line
的表,您的意思是results
我会这样做的
相关问题 更多 >
编程相关推荐