【问题标题】:How would you parse this HTML table using Python?你将如何使用 Python 解析这个 HTML 表格?
【发布时间】:2017-06-23 20:22:31
【问题描述】:

我正在尝试在 Python 2.7 中创建一个抓取脚本。

请求没问题,但我很难用 Beautiful soup 解析这张表。我尝试了很多,在论坛上搜索了很多,但对我没有任何效果,我第一次这样做。

代码如下:

 import requests, os 
 from bs4 import BeautifulSoup  

 url='http://fse.vdkruijssen.eu/ferrylist.php' params={'selectplane':'Cessna 208 Caravan','submit':''}
 response=requests.post(url, data=params) 

 soup = BeautifulSoup(response.text, "html5lib")
 table=soup.find('table')
 print table

但这并没有返回任何表格。我正在尝试至少检索第一列和最后一列。

【问题讨论】:

    标签: python-2.7 beautifulsoup html-parsing


    【解决方案1】:
    soup = BeautifulSoup(response.text, "lxml")
    

    将解析器更改为lxml

    Beautiful Soup 支持 Python 标准库中包含的 HTML 解析器,但它也支持许多第三方 Python 解析器。一个是 lxml 解析器。根据您的设置,您可以使用以下命令之一安装 lxml:

    $ apt-get install python-lxml
    
    $ easy_install lxml
    
    $ pip install lxml
    

    默认情况下,BS4 使用lxml 解析器。

    【讨论】:

    • 感谢您的回答和精确!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    • 2015-10-14
    • 2011-01-04
    相关资源
    最近更新 更多