Scrapy在回调函数中直接请求新页面XPath
作者:尐邪 发布于:2013-6-5 17:31 Wednesday 分类:芝麻绿豆
使用Scrapy抓取数据时,往往需要将多个页面的内容拼凑到一起然后保存
具体的需求情况就不啰嗦了
如下是简单的测试代码
#在函数中请求加载 新页面,片段代码
from scrapy.http import Request,HtmlResponse #载入相关类,如果头部有可以直接移除
from scrapy.selector import HtmlXPathSelector #同上
import urllib2 #同上
newurl = 'http://www.baidu.com' #新页面的url (必须是包含协议名称的完整url)
temp = urllib2.urlopen(newurl) #请求
temp = temp.read() #读数据
newresponse = HtmlResponse(newurl)
newresponse._set_body(temp)
temp = HtmlXPathSelector(newresponse) #构建新的xpath选择器
print temp.select('//title/text()').extract()[0] #测试
欢迎留言交流
发表评论: