3

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

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文件并将其保存在硬盘驱动器中,然后将文件提供给模块并获取图像。

我可以通过什么方式实现这一目标?

4

1 回答 1

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
于 2020-09-23T03:09:51.770 回答