抓取和抓取Wiki:当日图片

问题描述 投票:0回答:1

我正在尝试一个宠物项目,该项目需要我浏览Wikipedia列表:按月显示的每日页面图片。例如:https://en.wikipedia.org/wiki/Wikipedia:Picture_of_the_day/May_2004包含图像列表,后跟每个图像的简短标题。我想在这里做以下两件事:

  1. 从页面和相应标题中刮取所有图像。 (最好是用于存储图像的字典:字幕对)
  2. 爬行到其他月份并重复1。

非常感谢您提供有关如何完成此操作的帮助。

非常感谢。

python web-crawler screen-scraping wikipedia
1个回答
0
投票

我建议您在python中使用scrapy,因为它比f.e.要轻得多。硒。在函数解析中,您可以查找所有img标签,例如此处,在获取给定站点的html之后。在这里,您可以打印找到的所有图像和文本链接,因为我们需要的所有文本都在<p>标记中,或者如果需要,可以将它们保存到文件中。

import scrapy
from scrapy.crawler import CrawlerProcess
import logging

class Spider(scrapy.Spider):
   def __init__(self):
      self.name = "WikiScraper"
      self.start_urls = ["https://en.wikipedia.org/wiki/Wikipedia:Picture_of_the_day/May_2004"] # Here you can add more links or generate them
   def parse(self, response):
      for src in response.css('img::attr(src)').extract():
         print("Image:", src)
      for text in response.css('p *::text'):
         print("Text:", text.extract())

if __name__ == "__main__":
   logging.getLogger('scrapy').propagate = False
   process = CrawlerProcess()
   process.crawl(Spider)
   process.start()

最后,您必须将所有应该合并在一起的文本(我没有时间这样做)并添加您需要的所有网站。我没提到的所有其他内容都可以在scrapy上找到。

希望我什么都没错过!

© www.soinside.com 2019 - 2024. All rights reserved.