scrapy中将str转为能xpath的html格式
scrapy中将str转为能xpath的html格式在scrapy获取资源时,有时候获取的是json格式,但是json里面存在着html的标签,这时候就可以将json转为字典,然后再将html标签拿到,然后再进行xpath,就可以获取到标签内的资源了;
但是,怎么将这里的str转为能xpath的html标签呢,这里就用到了Selector
from scrapy.selector import Selector
names = Selector(text=datas).xpath("//div/a/text()").get()
这里的datas就是获取到的json里面的str(html标签的字符串格式),将datas转为html标签,然后再进行xpath就可以了。
上一篇也是在纠结xpath格式化srt发现的一坑
https://bbs.nightteam.cn/thread-277.htm
scrapy中将str转为能xpath的html格式
scrapy中将str转为能xpath的html格式
在scrapy获取资源时,有时候获取的是json格式,但是json里面存在着html的标签,这时候就可以将json转为字典,然后再将html标签拿到,然后再进行xpath,就可以获取到标签内的资源了;
但是,怎么将这里的str转为能xpath的html标签呢,这里就用到了Selector
from scrapy.selector import Selector
names = Selector(text=datas).xpath("//div/a/text()").get()
这里的datas就是获取到的json里面的str(html标签的字符串格式),将datas转为html标签,然后再进行xpath就可以了。
上一篇也是在纠结xpath格式化srt发现的一坑
https://bbs.nightteam.cn/thread-277.htm
from scrapy.selector import Selector
def parse(self, response):
for i in json.loads(response.text)['result']:
item = deepcopy(response.meta['item'])
div = Selector(text=i['div']) # 与解析josn和xpath类似,只需要加入这句,即可和之前版本兼容
item['title'] = i.get('Pname')
item['url'] = div.xpath('//a[@target]/text()').get()
yield deepcopy(item)
页:
[1]