Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

python - This gives me None as output

I am trying to parse src='the-link' from this HTML snippet. But I don't know how. Here is my code:

from bs4 import BeautifulSoup
import requests

source = requests.get('https://www.reddit.com/r/programmingmemes/').text
soup = BeautifulSoup(source, 'lxml')

img = soup.find('div', class_='_3Oa0THmZ3f5iZXAQ0hBJ0k')
div = img.find('div')
src = div.find('src')
print(src)

I want the output as:

<div> <img alt="Post image" class="_2_tDEnGMLxpM6uOa2kaDB3 ImageBox-image media-element _1XWObl-3b9tPy64oaG6fax" src="preview.redd.it/ik1g60hzoqc61.jpg? width=640&amp;crop=smart&amp;auto=webp&amp;s=c5fedaba3e5627cf8fcdd008317ac39789d71abc" style="max-height:512px"/> </div>
question from:https://stackoverflow.com/questions/65849025/this-gives-me-none-as-output

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are using the wrong class name, i think _2_tDEnGMLxpM6uOa2kaDB3 would be the right one (there just is no element on that site with class name _3Oa0THmZ3f5iZXAQ0hBJ0k)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...