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
130 views
in Technique[技术] by (71.8m points)

python - How can I use the find all () and () txt methods in the BeautifulSoup library?

I want all tags with a bargain price from the link given to the url. find. But when I use the find_all () method I use and want it as text ()

Show in the for loop, but I have the following output:

None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None

my code:

import requests
from bs4 import BeautifulSoup
import re

url = 'https://divar.ir/s/tehran/auto'
test_page = requests.get(url)
b = test_page.text

soup = BeautifulSoup(b,'html.parser')

c = soup.find_all('div',attrs="kt-post-card__top-description kt-post-card-description")

for v in c:
    print(v.txt)

Please help me solve this problem

question from:https://stackoverflow.com/questions/65642901/how-can-i-use-the-find-all-and-txt-methods-in-the-beautifulsoup-library

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

1 Answer

0 votes
by (71.8m points)

You can simply find all class attribute by using c = soup.find_all(class_="kt-post-card__top-description kt-post-card-description")

The following code will get the result

import requests
from bs4 import BeautifulSoup

url = 'https://divar.ir/s/tehran/auto'
test_page = requests.get(url).text
soup = BeautifulSoup(test_page,'html.parser')

c = soup.find_all(class_="kt-post-card__top-description kt-post-card-description")
for i in c:
    print(i.text)


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

...