简体   繁体   中英

Using BS4 to find specific Options

How would I use Beautifulsoup to find 7 ( <option value="NIC1633_9729">7</option> )

<option value="NIC1633_9729">7</option>
<option value="NIC1633_9730">7 1/8</option>
<option value="NIC1633_9731">7 1/4</option>
<option value="NIC1633_9732">7 3/8</option>
<option value="NIC1633_9733">7 1/2</option>
<option value="NIC1634_9734">7 5/8</option>
<option value="NIC1634_9735">7 3/4</option>
<option value="NIC1634_9736">7 7/8</option>

Thanks !

In the docs under Searching the Tree you can find information on locating elements via various methods. In your case you are looking for an <option> element with a text value of 7 . The docs mention that one of the ways you can find elements is by passing a string to find/findall . A little further down , it mentions that you can search for strings using the string parameter. So, based on what you've provided:

import bs4

html = """
<option value="NIC1633_9729">7</option>
<option value="NIC1633_9730">7 1/8</option>
<option value="NIC1633_9731">7 1/4</option>
<option value="NIC1633_9732">7 3/8</option>
<option value="NIC1633_9733">7 1/2</option>
<option value="NIC1634_9734">7 5/8</option>
<option value="NIC1634_9735">7 3/4</option>
<option value="NIC1634_9736">7 7/8</option>
"""

soup = bs4.BeautifulSoup(html,'html.parser')
print(soup.find("option",string = "7"))

The above code returns <option value="NIC1633_9729">7</option> .

The section concerning the string parameter also mentions that you can use regex (among other possible values) as the argument in case "7" is not matching exactly what is on the page that you are working off of.

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