使用Newspaper从HTML中提取图像



我不能像通常实例化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

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

我想这就是你想要的:

from newspaper import fulltext
html = 'your html'
text_from_html = fulltext(html)

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

然后,如果你在第一个版本(如下(中遇到SSL错误

/usr/local/lib/python3.8/site packages/urlib3/connectionpool.py:981:InsecureRequest警告:正在向主机"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对你没有多大好处,因为那样你在其他字段中不会得到任何东西。让我知道这是否解决了问题,或者是否出现了任何其他错误!

相关内容

  • 没有找到相关文章

最新更新