Quick list of the various search mechanisms to find elements when using BeautifulSoup.
The key thing to remember is that you can use multiple parameters to find a specific element. For example, you can combine in a method the tag, class_ and id to identify univocally the exact element you want.
Another quick thing worth mentioning is that the text parameter can receive and match a list that will be used to finding occurrences, and that also permits the use of regular expressions, when given in the re.compile('\w+') form.
- find(): find the first occurrence.
- find('p'): find the first occurrence of a tag (in this case, the HTML tag "p").
- find(text="sometext"): find the first occurrence of text.
- find(attrs={'id':'value'}): find the first occurrence of attributes based on the attribute value.
- find(class_='value'): find the first occurrence with the CSS class value.
- find_all(): find all occurrences based on filter conditions.
- find_parent(): find the first occurrence in parent tags.
- find_parents(): find all occurrences in parent tags.
- find_sibling(): find the first occurrence in sibling tags.
- find_siblings(): find all occurrences in sibling tags.
- find_next(): find the first occurrence of a tag parsed immediately after the current tag.
- find_all_next(): find occurrences of all tags parsed immediately after the current tag.
- find_previous(): find the first occurrence of a tag parsed immediately before the current tag.
- find_all_previous(): find occurrences of all tags parsed immediately before the current tag.
No comments:
Post a Comment