【问题标题】:How to parse specific HTML table from website on python如何在 python 上从网站解析特定的 HTML 表
【发布时间】:2020-02-15 15:32:00
【问题描述】:

我是使用 python 进行网页抓取的初学者。我正在尝试解析兰卡威岛的礼拜场所表。这是我指的网站http://www.jaik.gov.my/?page_id=658

我在python中输入了以下内容:-

import requests

import lxml.html as lh

import pandas as pd

langkawi_url = 'http://www.jaik.gov.my/?page_id=658'

page = requests.get(langkawi_url)

doc = lh.fromstring(page.content)

tr_elements = doc.xpath('//td')

[len(T) for T in tr_elements[:12]]

tr_elements = doc.xpath('//tr')

col = []
i = 0

for t in tr_elements[0]:
    i+=1
    name=t.text_content()
    print("%d:%s" % (i,name))
    col.append((name,[]))

显然我得到的输出是这样的:-

1:Sun
2:Mon
3:Tue
4:Wed
5:Thu
6:Fri
7:Sat

我希望得到这个:-

1:BIL
2:KARIAH MASJID
3:ALAMAT
4:MUKIM

非常感谢您的建议和指导。

谢谢!

【问题讨论】:

    标签: python web-scraping html-parsing


    【解决方案1】:

    尝试将您的代码更改为:

    tr_elements = doc.xpath('//td/strong')
    col = []
    for t in tr_elements:
        col.append(t.text)
    print(col)
    

    输出:

    ['BIL', 'KARIAH MASJID', 'ALAMAT', 'MUKIM']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-09
      • 2013-01-05
      • 2021-12-25
      • 2013-09-15
      • 2015-06-07
      • 2013-04-21
      • 2015-09-30
      相关资源
      最近更新 更多