Scrapy Yahoo小组爬虫
我在尝试抓取一个Y!小组的数据,能从一页上获取到信息,但就只能做到这一点。我有一些基本的规则,但显然这些规则不太对。有没有人已经解决了这个问题?
class YgroupSpider(CrawlSpider):
name = "yahoo.com"
allowed_domains = ["launch.groups.yahoo.com"]
start_urls = [
"http://launch.groups.yahoo.com/group/random_public_ygroup/post"
]
rules = (
Rule(SgmlLinkExtractor(allow=('message','messages' ), deny=('mygroups', ))),
Rule(SgmlLinkExtractor(), callback='parse_item'),
)
def parse_item(self, response):
hxs = HtmlXPathSelector(response)
sites = hxs.select('/html')
item = Item()
for site in sites:
item = YgroupItem()
item['title'] = site.select('//title').extract()
item['pubDate'] = site.select('//abbr[@class="updated"]/text()').extract()
item['desc'] = site.select("//div[contains(concat(' ',normalize-space(@class),' '),' entry-content ')]/text()").extract()
return item
1 个回答
0
看起来你对自己在做什么几乎没有概念。我对Scrapy也很新,但我觉得你需要写类似这样的代码:Rule(SgmlLinkExtractor(allow=('http\://example\.com/message/.*\.aspx', )), callback='parse_item'),
。试着写一个正则表达式,来匹配你想要的完整链接地址。另外,看起来你只需要一个规则。把回调函数加到第一个规则里。链接提取器会匹配所有符合允许条件的链接,然后排除掉不符合的链接,接着剩下的每个页面都会被加载,并传递给parse_item
。
我说这些的时候其实对你要抓取的页面和你想要的数据并不了解。你需要这种爬虫是因为那个页面上有指向你想要数据的链接。