我正在构建一个新闻策划服务,该服务使用来自各种来源的RSS源,包括The Guardian。
当我尝试从“卫报”文章中提取图像时,我得到:错误401未找到签名错误。
但是,当您将文章分享到Facebook等时,图像将显示在Feed中。
有没有办法让我像Facebook一样使用图像?
感谢。
答案 0 :(得分:0)
您遇到的401错误可能是因为您尝试使用某些Intranet资源而未在系统中进行记录或身份验证。
使用以下代码,您将能够获取较小版本的图片。它将读取您提供的页面的html源代码并搜索具有特定要求的img
<强>代码:强>
from bs4 import BeautifulSoup
import requests
url = 'https://www.theguardian.com/global-development/2016/mar/22/world-water-day-quiz-are-you-a-fount-of-wisdom'
html_source = requests.get(url).text
#print(html_source)
soup = BeautifulSoup(source, 'html.parser')
img = soup.find_all('img', {'class':'maxed responsive-img'})
然后你可以print
结果:
只有第一个img:
print(img[0]['src'])
输出:
https://i.guim.co.uk/img/media/dd92773d05e7da9adcff7c007390a746930c2f71/0_0_2509_1505/master/2509.jpg?w=300&q=55&auto=format&usm=12&fit=max&s=ba3a4698fe5fce056174eff9ff3863d6
所有img结果:
for i in img:
print(i['src'])
输出:
https://i.guim.co.uk/img/media/dd92773d05e7da9adcff7c007390a746930c2f71/0_0_2509_1505/master/2509.jpg?w=300&q=55&auto=format&usm=12&fit=max&s=ba3a4698fe5fce056174eff9ff3863d6
https://i.guim.co.uk/img/media/6ef58c034b1e86f3424db4258e398c88bb3a3fb4/0_0_5200_3121/2000.jpg?w=300&q=55&auto=format&usm=12&fit=max&s=ea8370295d1e2d193136fd221263c8b8
https://i.guim.co.uk/img/media/e1c2b1336979a752a68c3c554611bc28aa0a4baa/0_290_4324_2594/2000.jpg?w=300&q=55&auto=format&usm=12&fit=max&s=eef138cefe66834919c3544826a3e468
https://i.guim.co.uk/img/media/37df4e7b52dfd554d431f7d439cdd1a137789fa4/0_0_4256_2553/2000.jpg?w=300&q=55&auto=format&usm=12&fit=max&s=9e461f6739325cf3524a1228f5f7e60b