Created
June 30, 2013 16:58
-
-
Save yejianye/5895954 to your computer and use it in GitHub Desktop.
lxml cssselect example
This file contains hidden or 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
import lxml | |
root = lxml.html.parse(filename).getroot() | |
# in case the file contains unicode characters | |
parser = lxml.html.HTMLParser(encoding='utf-8') | |
root = lxml.html.parse(filename, parser=parser).getroot() | |
# get matched elements using css selector | |
els = root.cssselect('div.shop-info div.info-name h2 a') | |
el_text = els[0].text | |
el_href = els[0].attrib['href'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment