Issue
Ive successfully created a python script that can print all image paths from a specified url:
from requests_html import HTMLSession
from urllib.request import urlopen
from bs4 import BeautifulSoup
import requests
url="https://www.example.com/"
session = HTMLSession()
r = session.get(url)
b = requests.get(url)
soup = BeautifulSoup(b.text, "lxml")
images = soup.find_all('img')
for img in images:
if img.has_attr('src') :
print(img['src'])
What i now want to do is print the image size alongside the printed url using PIL. Ive tried this but it errors:
from requests_html import HTMLSession
from urllib.request import urlopen
from bs4 import BeautifulSoup
from PIL import Image
import requests
url="https://www.example.com/"
session = HTMLSession()
r = session.get(url)
b = requests.get(url)
soup = BeautifulSoup(b.text, "lxml")
images = soup.find_all('img')
for img in images:
if img.has_attr('src') :
## Get image sizes in PIL
imgsize = Image.open(requests.get(img, stream=True).raw)
print(img['src'], imgsize.size)
Any ideas how to get this working?
Solution
You should use img['src']
instead of img
requests.get(img['src'], ...).raw
Answered By - furas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.