BeautifulSoup4无法从表中抓取数据

维品

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

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字典上获取出站航班的工作代码

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"}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

BeautifulSoup4无法从此表中抓取数据

无法使用BeautifulSoup4抓取网站

使用python和Beautifulsoup4从抓取数据中写入和保存CSV文件

如何使用python和beautifulsoup4循环抓取网站中多个页面的数据

使用BeautifulSoup4进行数据抓取的问题

从抓取的数据中分割html(Python + BeautifulSoup4)

BeautifulSoup4表

使用 Python 抓取 HTML 中的特定元素:BeautifulSoup4

如何使用pd.DataFrame方法从使用beautifulsoup4抓取的信息中手动创建数据框

在没有唯一类的情况下BeautifulSoup4表抓取-学习

使用BeautifulSoup4在Python中存储标签中的数据

无法使用BeautifulSoup4(初学者)抓取正确的Wikitable

使用python + beautifulSoup4从动态图中抓取数据

循环不适用于使用python和beautifulsoup4抓取数据

无法从 BeautifulSoup4 中的字符串解析“href”

无法在Mac OS上的python中安装beautifulsoup4

如何使用BeautifulSoup4从客户标签中抓取信息

如何使用BeautifulSoup4使用Python修复Web抓取中的错误

在python 3.6中使用beautifulsoup4抓取网站以获取产品信息时

使用beautifulsoup4后如何分离抓取结果?

用BeautifulSoup4解析HTML表

Beautifulsoup4不显示表内容

Python Beautifulsoup4解析多个表

如何使用 Python 和 BeautifulSoup 从 html 表中抓取数据?

ImportError:无法导入名称“ BeautifulSoup4”

使用BeautifulSoup4解析数据

如何使用beautifulsoup在h4中抓取数据?

无法使用BeautifulSoup4(Python 3)刮擦特定的表

从表中抓取数据