【问题标题】:Web scraping by tag on stack overflow堆栈溢出时按标签抓取网页
【发布时间】:2019-07-10 10:00:57
【问题描述】:

我想在这个网站 (stackoverflow.com) 上进行网络抓取,我想知道是否有 API 或其他可以与 Python 一起使用的工具来获取包含特定标签的所有 cmets。

例如,我如何从10/01/2019 to 01/20/2019 获取带有python 标签的所有帖子和cmets?

【问题讨论】:

标签: python web-scraping topic-modeling stackexchange-api stackexchange


【解决方案1】:

详细看https://api.stackexchange.com/docs/

您可以使用questions 方法获取从开始日期到结束日期的所有带有特定标签的问题。您需要将特定标签传递到tagged 参数中。

以下是网址格式:
https://api.stackexchange.com/2.2/questions?fromdate={start_date}&todate={end_date}&order=desc&sort=activity&tagged={tag}&site=stackoverflow

例如,以下链接返回 2019 年 7 月 1 日至 2019 年 7 月 5 日的所有问题,标签为 python:
https://api.stackexchange.com/2.2/questions?fromdate=1561939200&todate=1562284800&order=desc&sort=activity&tagged=python&site=stackoverflow

有关上述 URL 中日期格式的更多信息,您可以查看dates

现在您有了question_id,您可以使用questions/{ids}/answers 方法获取该问题从开始日期到结束日期的所有答案。

以下是网址格式:
https://api.stackexchange.com/2.2/questions/{question_id}/answers?fromdate={start_date}&todate={end_date}&order=desc&sort=activity&site=stackoverflow

例如,下面的链接会将 2019 年 1 月 1 日至 2019 年 7 月 1 日期间的所有答案返回到 question_id 37181281:
https://api.stackexchange.com/2.2/questions/37181281/answers?fromdate=1546300800&todate=1561939200&order=desc&sort=activity&site=stackoverflow

现在您基本上拥有了从开始日期到结束日期的所有帖子(问题和答案),并带有特定的标签。

由于您有帖子的question_idanswer_id,您可以使用questions/{ids}/comments 方法和answers/{ids}/comments 方法来获取这些帖子上的cmets。

【讨论】:

  • 非常感谢!如果我能得到你的空闲时间,我很想问你最后一个问题,如果我想获得不同年份的大量数据。比如从2016年到2019年,如何滚动页面并增加每页的最大输出?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-30
  • 2014-05-02
  • 2021-05-28
相关资源
最近更新 更多