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

python - How can get Net Seller by using beautifulsoup

    enter code here

<div class="_1hVBfR">
   <span class="row _1kkfO3 BqOr_g">Google Nest Mini (2nd Gen) with Google A...</span>
   <div class="row _65ZkAB">
      <span class="_3dOR_a">
      <span class="_65ZkAB">Color: </span>
      <span class="-igymY">Black</span></span>
      <span class="_3dOR_a"></span>
   </div>
   <div class="row _65ZkAB">
      <span class="_65ZkAB _1uABhR">Seller: </span>
      <span class="-igymY">Net Seller</span>
   </div>
</div>

How can i get net seller as the output value. Since the class name is same for both Black and Net seller i am getting only black.

question from:https://stackoverflow.com/questions/65713428/how-can-get-net-seller-by-using-beautifulsoup

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

1 Answer

0 votes
by (71.8m points)

You could use soup.find_all() to select all span then select desire output using index.

from bs4 import BeautifulSoup

html = '''<div class="_1hVBfR">
   <span class="row _1kkfO3 BqOr_g">Google Nest Mini (2nd Gen) with Google A...</span>
   <div class="row _65ZkAB">
      <span class="_3dOR_a">
      <span class="_65ZkAB">Color: </span>
      <span class="-igymY">Black</span></span>
      <span class="_3dOR_a"></span>
   </div>
   <div class="row _65ZkAB">
      <span class="_65ZkAB _1uABhR">Seller: </span>
      <span class="-igymY">Net Seller</span>
   </div>
</div>'''

soup = BeautifulSoup(html, 'lxml')

res = soup.find_all('span', {'class': '-igymY'})[1]

print(res.text)

res = soup.find_all('span', {'class': '-igymY'})

print([t.text  for t in res if t.text == 'Net Seller'][0])

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

...