Created
September 13, 2013 06:10
-
-
Save tomotaka/6547194 to your computer and use it in GitHub Desktop.
BeautifulSoup samples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
#from bs4 import BeautifulSoup | |
#soup = BeautifulSoup(some_html_string) | |
def get_string_by_id(soup, _tag, _id): | |
tag = soup.find(_tag, id=_id) | |
return tag.string if tag is not None else None | |
def get_string_by_class(soup, _tag_name, _class): | |
tag = soup.find(_tag_name, class_=_class) | |
return tag.string if tag is not None else None | |
def get_input_value_by_name(soup, input_name): | |
input_tag = soup.find('input', attrs={'name': input_name}) | |
return input_tag.attrs['value'] is not None else None | |
def get_textarea_value_by_name(soup, textarea_name): | |
textarea_tag = soup.find('textarea', attrs={'name': textarea_name}) | |
return textarea_tag.string if textarea_tag is not None else None | |
def get_checked_value_of_radio(soup, check_name): | |
radio_tags = soup.find_all('input', attrs={'type': 'radio', 'name': check_name}) | |
for radio_tag in radio_tags: | |
if hasattr(radio_tag, 'attrs') and 'checked' in radio_tag.attrs: | |
return radio_tag.attrs['value'] | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment