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()
. Я также не уверен, правильна ли моя логика. Plz помогите мне поместить данные таблицы.
Я бы сделал это так
import pandas as pd
result = pd.read_html("https://pqt.cbp.gov/report/YYZ_1/12-01-2017")
df = result[0]
# df = df.drop(labels='Unnamed: 8', axis=1)
df.to_csv(r'C:\Users\User\Desktop\Data.csv', sep=',', encoding='utf-8',index = False )
переменная r
- тип Response
not str
, используйте r.text
или r.content
и нет таблицы с table-horizontal-line
классов table-horizontal-line
, вы имеете в виду results
?
soup = BeautifulSoup(r.text)
table = soup.find('table', attrs={"class" : "results"})
пытаться:
r = requests.get('https://pqt.cbp.gov/report/YYZ_1/12-01-2017')
soup = BeautifulSoup(r.content)
headers = [header.text for header in table.find_all('th')]
AttributeError: 'NoneType' object has no attribute 'find_all''