简体   繁体   中英

Beautifulsoup - How can I get the li text without select?

How can I gtab the "get this li" text in this HTML code in python with beautifulsoup without find.select, because this row will change on other pages.

<ul id="product-attribute-table">
<li>
Brand:N/A </li>
<li>
Pack Available:N/A </li>
<li>
Get this li </li>
<li>
Colour:N/A </li>
<li>
Display Type:N/A </li>
<li>
Retail Price:N/A </li>
</ul>

My try:

ul = soup.find('ul', {'id': 'product-attribute-table'} #this grab the ul with this id

how can I search this li, which contains "Get this li" text?

You can try

li = [i for i in soup.find_all('li') if i.text.strip() == 'Get this li']

OR

li = [i for i in soup.find_all('li') if 'Get this li' in i.text]

the li will be a list with the li item that has the text 'Get this li' .

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