使用Python获取实际的Facebook和Twitter图像URL [英] Getting actual facebook and twitter image urls using python
问题描述
我想编写一个python代码,从包含图片的网址中下载主"图片.
I want to write a python code that downloads 'main' image from urls that contain images.
我的数据(文本文件)中有这样的网址
I have urls like these in my data (text files)
指向fb图片
指向Twitter图片
points to twitter image
,但其扩展的网址不会产生.jpg,.png图片.相反,他们将我们定向到包含所需图像的页面.
but their expanded urls don't result in .jpg,.png images. Instead they direct us to a page that contains the desired image.
如何从这些URL下载图像?
How do I download images from these urls?
推荐答案
在这里,您将找到一个示例,说明我如何从Facebook页面下载飞机图像,您可以对其进行调整以使其适用于您的Twitter页面:
Here you will find an example of how I downloaded the plane image from the facebook page, you can adapt this to work for your twitter page:
from bs4 import BeautifulSoup
import urllib
urlData = urllib.urlopen('https://www.facebook.com/photo.php?fbid=10152055005350906')
data = str(urlData.readlines())
bs = BeautifulSoup(data)
imgUrl = bs.find('img', attrs={'class': 'fbPhotoImage img'}).get('src')
urllib.urlretrieve(imgUrl, "plane.jpg")
编辑
我决定也实际帮助您处理Twitter,这是从您提供的链接下载图像的Twitter示例:
EDIT
I decided to actually help you out with the twitter one as well, here is the twitter example of downloading the image from the link you gave:
from bs4 import BeautifulSoup
import urllib
urlData = urllib.urlopen('https://twitter.com/USABillOfRights/status/468852515409502210/photo/1')
data = str(urlData.readlines())
bs = BeautifulSoup(data)
imgUrl = bs.find('img', attrs={'alt': 'Embedded image permalink'}).get('src')
urllib.urlretrieve(imgUrl, "cnn.jpg")
这篇关于使用Python获取实际的Facebook和Twitter图像URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!