【问题标题】:Extract image using Newspaper from HTML使用报纸从 HTML 中提取图像
【发布时间】:2020-12-29 17:04:14
【问题描述】:

我无法像通常那样下载文章来实例化 Article 对象,如下所示:

from newspaper import Article
url = 'http://fox13now.com/2013/12/30/new-year-new-laws-obamacare-pot-guns-and-drones/'
article = Article(url)
article.download()
article.top_image

但是,我可以从请求中获取 HTML。我可以使用这个原始 HTML 并以某种方式将其传递给 Newspaper 以从中提取图像吗? (以下是尝试,但不起作用)。谢谢

from newspaper import Article
import requests
url = 'http://fox13now.com/2013/12/30/new-year-new-laws-obamacare-pot-guns-and-drones/'
raw_html= requests.get(url, verify=False, proxies=proxy)
article = Article('')
article.set_html(raw_html)
article.top_image

【问题讨论】:

  • 为什么不起作用?你得到哪个错误?
  • 我无法将公司的内部 SLL 证书密钥注入到我的请求中。该问题正在调查中。唯一的解决方法是手动发出请求并传递verify=False,这给了我原始 HTML

标签: python extract python-newspaper newspaper3k


【解决方案1】:

Python 模块 Newspaper 允许使用代理,但此功能未在模块文档中列出。


报纸代理

from newspaper import Article
from newspaper.configuration import Configuration

# add your corporate proxy information and test the connection
PROXIES = {
           'http': "http://ip_address:port_number",
           'https': "https://ip_address:port_number"
          }

config = Configuration()
config.proxies = PROXIES

url = 'http://fox13now.com/2013/12/30/new-year-new-laws-obamacare-pot-guns-and-drones/'
articles = Article(url, config=config)
articles.download()
articles.parse()
print(articles.top_image)
https://ewscripps.brightspotcdn.com/dims4/default/d49dab0/2147483647/strip/true/crop/400x210+0+8/resize/1200x630!/quality/90/?url=http%3A%2F%2Fmediaassets.fox13now.com%2Ftribune-network%2Ftribkstu-files-wordpress%2F2012%2F04%2Fnational-news-e1486938949489.jpg

代理和报纸的请求

import requests
from newspaper import Article

url = 'http://fox13now.com/2013/12/30/new-year-new-laws-obamacare-pot-guns-and-drones/'
raw_html = requests.get(url, verify=False, proxies=proxy)
article = Article('')
article.download(raw_html.content)
article.parse()
print(article.top_image) https://ewscripps.brightspotcdn.com/dims4/default/d49dab0/2147483647/strip/true/crop/400x210+0+8/resize/1200x630!/quality/90/?url=http%3A%2F%2Fmediaassets.fox13now.com%2Ftribune-network%2Ftribkstu-files-wordpress%2F2012%2F04%2Fnational-news-e1486938949489.jpg

【讨论】:

    【解决方案2】:

    首先确保您使用的是python3,并且您之前运行过pip3 install newspaper3k

    如果您在第一个版本中遇到 SSL 错误(如下所示)

    /usr/local/lib/python3.8/site-packages/urllib3/connectionpool.py:981:InsecureRequestWarning:正在向主机“fox13now.com”发出未经验证的 HTTPS 请求。强烈建议添加证书验证。见:https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings 警告.warn(

    您可以通过添加禁用它们

    import urllib3
    urllib3.disable_warnings()
    

    这应该可行:

    from newspaper import Article
    import urllib3
    urllib3.disable_warnings()
    
    
    url = "https://www.fox13now.com/2013/12/30/new-year-new-laws-obamacare-pot-guns-and-drones/"
    article = Article(url)
    article.download()
    print(article.html)
    

    使用python3 <yourfile>.py 运行。


    您自己在文章中设置 html 对您没有多大好处,因为您不会在其他领域得到任何东西。让我知道这是否解决了问题,或者是否弹出任何其他错误!

    【讨论】:

    • 我无法使用报纸下载的原因是因为我使用了公司代理。我尝试了多种方式注入 SSL 证书。我现在可以通过的唯一方法是在请求中使用verify=False,这显然必须更改。我可以在原始 HTML 上运行 Newspaper 的 summary,所以我的直觉是我也应该能够使用原始 HTML 获取图像。
    • 啊,事情变得复杂了。可以使用全文吗? from newspaper import fulltext; html = requests.get(...).text; text = fulltext(html)
    • 是的,我能做到。如果您运行第二个 sn-p 代码,您应该能够测试 Article 中的哪些函数可以在原始 HTML 中工作,哪些不能。
    • 其他选项可能是添加文章的自定义版本(请参阅this blog 上的最后一个代码块。
    猜你喜欢
    • 1970-01-01
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-15
    • 2015-11-22
    • 1970-01-01
    • 2013-06-30
    相关资源
    最近更新 更多