当天的抓取和抓取 Wiki:Picture

Crawling and Scraping Wiki:Picture of the day

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

  1. 从页面和相应的标题中抓取所有图像。 (最好是一个字典来存储图像:标题对)
  2. 爬取其他月份并重复 1。

如能提供有关如何实现此目的的任何帮助,我们将不胜感激。

非常感谢。

我建议您在 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.

上找到

希望我没有遗漏任何东西!