【问题标题】:Extract tables in webpages from Python/R or other software从 Python/R 或其他软件中提取网页中的表格
【发布时间】:2022-01-19 05:12:31
【问题描述】:

我想从网站中提取姓名、学校地址、电话、传真、校长: https://www.edb.gov.hk/en/student-parents/sch-info/sch-search/schlist-by-district/school-list-cw.html 有可能吗?

【问题讨论】:

  • 到目前为止你有没有尝试过?

标签: python r


【解决方案1】:

是的,这是可能的,并且有许多工具可以帮助您做到这一点。如果您不想使用编程语言,则可以使用大量工具(但可能需要付费,这里有一篇文章可能有用:https://popupsmart.com/blog/web-scraping-tools)。 但是,如果你想使用 python,你应该做的是加载页面然后解析 HTML。然后你应该看看你想要的元素并获取它的数据。本文用代码解释了整个过程:https://www.freecodecamp.org/news/web-scraping-python-tutorial-how-to-scrape-data-from-a-website/

这是一个简单的代码,显示了您发布的页面中的表格,基于上述论文中的代码:

import requests
from bs4 import BeautifulSoup
# Make a request
page = requests.get(
    "https://www.edb.gov.hk/en/student-parents/sch-info/sch-search/schlist-by-district/school-list-cw.html")
soup = BeautifulSoup(page.content, 'html.parser')

# Create top_items as empty list
top_items = []

# Extract and store in top_items according to instructions on the left
products = soup.select('table')
for elem in products:
 print(elem)

您可以在这里试用: https://colab.research.google.com/drive/13EzFWNBqpkGf4CZvGt5pYySCuW7Ij6I4?usp=sharing

【讨论】:

  • 您的评论很有帮助。
  • 很高兴它有帮助。您可以将其标记为答案,以帮助其他人知道问题已得到解决。
猜你喜欢
  • 1970-01-01
  • 2021-11-29
  • 1970-01-01
  • 2021-06-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多