gpt4 book ai didi

python - BeautifulSoup4 无法从表中抓取数据

转载 作者:太空宇宙 更新时间:2023-11-04 02:52:09 26 4
gpt4 key购买 nike

我想抓取站点表的第 2 列和第 3 列 https://www.airvistara.com/fly/flightschedule我使用的代码是

import bs4 as bs
from urllib2 import urlopen

sauce=urlopen('https://www.airvistara.com/fly/flightschedule').read()
soup=bs.BeautifulSoup(sauce,'lxml')
table=soup.table
table_body=table.find('tbody')
table_rows=table_body.find_all('tr')
for tr in table_rows:
td=tr.find_all('td')
row=[i.text for i in td]
print row

但是我找不到想要的解决方案

最佳答案

您尝试解析的内容是通过 ajax 加载的,bs 无法使用它。
这是在 python 字典上获取 Outbound Flights 的工作代码:

import json
import requests

post_fields = {"flightDate":"22/04/2017"}
headers = {'content-type': 'application/json'}
url = 'https://www.airvistara.com/fly/getFlightschedule'
json_response = requests.post(url, data=json.dumps(post_fields), headers=headers).text
decoded_json = json.loads(json_response)
print decoded_json

输出:

{u'flightSchedule': [{u'effectiveFrom': u'19-APR-2017', u'flightCode': u'UK 0946', u'baseFareL1': 0, u'flightDate': u'Saturday, 28 October 2017',...

要获取每个航类的详细信息,您可以使用:

for flight in decoded_json['flightSchedule']:
print flight['effectiveFrom']
print flight['flightCode']
print flight['baseFareL1']
print flight['flightDate']
print flight['daysOfOperation']
print flight['arrivalStation']
print flight['departureStation']
print flight['via']
print flight['scheduledArrivalTime']
print flight['departureCityName']
print flight['effectiveTo']
print flight['arrivalCityName']
print flight['scheduledDepartureTime']

输出结果如下:

19-APR-2017
UK 0946
0
Saturday, 28 October 2017
Daily
DEL
AMD
-
10:25
Ahmedabad
28-OCT-2017
New Delhi
08:45

注意事项:
1 - 如果您需要指定 arrivalStationdepartureStation,请使用:

post_fields = {"flightDate":"22/04/2017","arrivalStation":"AIRPORTCODE","departureStation":"AIRPORTCODE"}

关于python - BeautifulSoup4 无法从表中抓取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43563966/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com