我想用漂亮的汤来提取this page上的关键信息。
我特别希望.booking-item
内的.booking-list
/ s。
期望的结果是:
[{"FERRY TYPE": "Traghetto", "DEPARTURE TIME":"06:40", "DEPARTURE DATE": "25/02/2019","DEPARTURE PORT": "CAPRI","ARRIVAL TIME":"06:40", "ARRIVAL DATE": "25/02/2019","ARRIVAL PORT": "NAPOLI PORTA DI MASSA"}]
我尝试了以下
caremar_departures_table = soup.select('.booking-item .row')
for div in caremar_departures_table:
caremar_timetable.append({
"FERRY TYPE": div[5].b.text,
"DEPARTURE TIME": div[12].h5.text
...
}
但是我得到了错误
return self.attrs[key]
KeyError: 5
答案 0 :(得分:1)
您可以使用特殊标记来分析站点。它更容易阅读。我帮你检查了。
from urllib import urlopen
from bs4 import BeautifulSoup
quote_page = "https://shop.caremar.it/main_acquista_1_corse_00_ajax.asp?l=it&data=25/02/2019&portoP=1&portoA=4&form_url=ticket_s1_2"
page = urlopen(quote_page)
soup = BeautifulSoup(page, "html.parser")
div = soup.findAll('div', attrs={'class': 'booking-item'})
caremar_timetable = []
for a in div:
ferry_type = a.find('b').text
departture_time = a.findAll('h5')[0].text
departure_date = a.findAll('p')[0].text
departure_port = a.findAll('b')[1].text
arrival_time = a.findAll('h5')[1].text
arrival_date = a.findAll('p')[2].text
arriva_port = a.findAll('p',attrs={'class' : 'booking-item-destination'})[1].text
arriva_port = " ".join(arriva_port.split())
caremar_timetable.append({
"FERRY TYPE": ferry_type,
"DEPARTURE TIME": departture_time,
"DEPARTURE DATE" : departure_date,
"DEPARTURE PORT" : departure_port,
"ARRIVAL TIME" : arrival_time,
"ARRIVAL DATE" : arrival_date,
"ARRIVAL PORT" : arriva_port
})
print caremar_timetable
您可以使用a.findAll('tag')[index]
或找到像a.findAll('p',attrs={'class' : 'booking-item-destination'})[1]
这样的特殊类
输出:
[{'ARRIVAL PORT': u'NAPOLI Porta di Massa', 'FERRY TYPE': u'TRAGHETTO',
'DEPARTURE DATE': u'Lun\xa025/02/2019', 'ARRIVAL TIME': u'08:00', 'DEPARTURE TIME': u'06:40', 'DEPARTURE PORT': u'CAPRI', 'ARRIVAL DATE': u'Lun\xa025/02/2019'},
{'ARRIVAL PORT': u'NAPOLI Porta di Massa', 'FERRY TYPE': u'TMV', 'DEPARTURE DATE': u'Lun\xa025/02/2019', 'ARRIVAL TIME': u'11:20', 'DEPARTURE TIME': u'10:20', 'DEPARTURE PORT': u'CAPRI', 'ARRIVAL DATE': u'Lun\xa025/02/2019'},
{'ARRIVAL PORT': u'NAPOLI Porta di Massa', 'FERRY TYPE': u'TRAGHETTO', 'DEPARTURE DATE': u'Lun\xa025/02/2019', 'ARRIVAL TIME': u'12:25', 'DEPARTURE TIME': u'11:00', 'DEPARTURE PORT': u'CAPRI', 'ARRIVAL DATE': u'Lun\xa025/02/2019'},
{'ARRIVAL PORT': u'NAPOLI Porta di Massa', 'FERRY TYPE': u'TMV', 'DEPARTURE DATE': u'Lun\xa025/02/2019', 'ARRIVAL TIME': u'16:25', 'DEPARTURE TIME': u'15:35', 'DEPARTURE PORT': u'CAPRI', 'ARRIVAL DATE': u'Lun\xa025/02/2019'},
{'ARRIVAL PORT': u'NAPOLI Porta di Massa', 'FERRY TYPE': u'TRAGHETTO', 'DEPARTURE DATE': u'Lun\xa025/02/2019', 'ARRIVAL TIME': u'19:00', 'DEPARTURE TIME': u'17:40', 'DEPARTURE PORT': u'CAPRI', 'ARRIVAL DATE': u'Lun\xa025/02/2019'},
{'ARRIVAL PORT': u'NAPOLI Porta di Massa', 'FERRY TYPE': u'TMV', 'DEPARTURE DATE': u'Lun\xa025/02/2019', 'ARRIVAL TIME': u'20:45', 'DEPARTURE TIME': u'20:05', 'DEPARTURE PORT': u'CAPRI', 'ARRIVAL DATE': u'Lun\xa025/02/2019'}]