Created
September 30, 2012 18:29
-
-
Save barraponto/3808079 to your computer and use it in GitHub Desktop.
The is a re-implementation of the Scrapy spider tutorial using HtmlCSSSelector
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
from scrapy.spider import BaseSpider | |
from scrapy.selector import HtmlCSSSelector | |
class DmozSpiderCSS(BaseSpider): | |
name = "pyquery" | |
allowed_domains = ["dmoz.org"] | |
start_urls = [ | |
"http://www.dmoz.org/Computers/Programming/Languages/Python/Books/", | |
"http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/" | |
] | |
def parse(self, response): | |
hcs = HtmlCSSSelector(response) | |
sites = hcs.select('ul li') | |
for site in sites: | |
links = site.select('a') | |
if len(links): | |
title = links[0].text_content() | |
link = links[0].get('href') | |
desc = site.text_content() | |
print title, link, desc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think some method same name with jquery may be better。ex: links[0].text() links[0].attr('href')。