python报纸模块 - 从文章中获取所有图像

时间:2018-06-05 19:21:10

标签: python django web-scraping python-newspaper

通过使用python的newspaper模块,我可以通过以下方式从文章中获取顶部图像:

from newspaper import Article
first_article = Article(url="http://www.lemonde.fr/...", language='fr')
first_article.download()
first_article.parse()
print(first_article.top_image)

但我需要获得文章中的所有图片。他们的github文档说:'可以从html' 中提取所有图像。但我无法解决这个问题。我不想手动下载并将html文件保存在硬盘中,然后用文件提供模块并获取图像。

我能以什么方式实现这一目标?

1 个答案:

答案 0 :(得分:0)

您可能已经解决了此问题,但是可以通过调用article.images与 Newspaper 获得图像URL。

from newspaper import Article

article = Article(url="http://www.lemonde.fr/", language='fr')
article.download()
article.parse()
top_image = article.top_image
all_images = article.images
for image in all_images:
  print(image)
   
  https://img.lemde.fr/2020/09/22/0/3/4485/2990/220/146/30/0/a79897c_115736902-000-8pt8nc.jpg
  https://img.lemde.fr/2020/09/22/0/0/5315/3543/192/0/75/0/7b90c88_645792534-pns-3418491.jpg
  https://img.lemde.fr/2020/09/09/200/0/1500/999/180/0/95/0/d8099d2_51464-3185927.jpg
  https://img.lemde.fr/2020/09/22/0/4/4248/2832/664/442/60/0/557e6ee_5375150-01-06.jpg