简体   繁体   中英

I have problem web scraping image; when I try my code, it just says []

This is my code:

import requests
from bs4 import BeautifulSoup
r = requests.get('https://www.reddit.com/r/memes/')
soup = BeautifulSoup(r.content,'html.parser')
post = soup.find_all('img',attr={'alt:Post image'})

The output is [] , but I don't know why.

As @jonrsharpe pointed out in the comments, you're passing a set as the target attribute rather than a dictionary. Simply remove the colon from inside of the string to set the key and value for the dict:

post = soup.find_all('img',attr={'alt':'Post image'})

after that, you can use img[src] to get the src link, open the link with a module like urllib2 and download the contents however you please.

Hope this helped

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM