xpath获取不了数据

来源:3-8 解析目标站点的详情页数据

mfloat流风

2020-02-19 23:17:04

# 代码

def parse(self, response):
   if '中为你找到0辆好车' in response.text:
       return None
   print(response.text)
   car_item_list = response.xpath("//ul[@class='carlist clearfix js-top']/li")
   print('car_item_list:%s'%car_item_list)
   car_list= []
   for car_item in car_item_list:
       car_info = {}
       # scrapy xptah返回的是一个列表,通过extract_first()方法
       car_info['car_name'] = car_item.xpath('./a/h2/text()').extract_first()
       car_info['car_detail_url'] = 'https://www.goazi.com' + car_item.xpath('./a/@href').extract_first()
       car_list.append(car_info)
   print("car_list:%s"%car_list)


#控制台输出


<input style="display: none;" name="js-waf-seo-referer" type="text" value="" />
<input type="text" name="history" style="display: none;" value='{"city":"102089","brand":"1199"}'>


<!-- <div class="kefu-container j-open-im" data-gzlog="tracking_type=click&eventid=92601051"></div>
 -->


<input type="hidden" id="appDownFlag" value="1"/>

<input type="hidden" id="isDisplayFinance" value="1"/>

<input type="hidden" id="isYanXuanNew" value="0"/>
<input type="hidden" id="hasVideoAd" value="0"/>

<input type="hidden" id="listStyleVersion" value="B"/>
<!-- seo growio -->

<!-- 百度熊掌号 -->


</body>
</html>
2020-02-19 23:14:41 [scrapy.core.engine] INFO: Closing spider (finished)
2020-02-19 23:14:41 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 1077,
 'downloader/request_count': 3,
 'downloader/request_method_count/GET': 3,
 'downloader/response_bytes': 22166,
 'downloader/response_count': 3,
 'downloader/response_status_count/200': 1,
 'downloader/response_status_count/203': 1,
 'downloader/response_status_count/302': 1,
 'elapsed_time_seconds': 6.381326,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2020, 2, 19, 15, 14, 41, 987860),
 'log_count/DEBUG': 2,
 'log_count/INFO': 10,
 'response_received_count': 1,
 'scheduler/dequeued': 3,
 'scheduler/dequeued/memory': 3,
 'scheduler/enqueued': 3,
 'scheduler/enqueued/memory': 3,
 'start_time': datetime.datetime(2020, 2, 19, 15, 14, 35, 606534)}
2020-02-19 23:14:41 [scrapy.core.engine] INFO: Spider closed (finished)
car_item_list:[]
car_list:[]

Process finished with exit code 0


写回答

2回答

好帮手乔木

2020-02-20

同学你好:

可能是同学的代码写错了:

parse()方法在scrapy中会直接调用,同学可以更换一个函数名称,然后参考对比下面的代码:

class GuaziSpider(scrapy.Spider):
    name = 'guazi'
    allowed_domains = ['guazi.com']
    # start_urls = ['http://guazi.com/']
    def start_requests(self):
        while True:
            task = mongo.get_task('guazi_task')
            #task取空了,就停下来
            if not task:
                break
            if '_id' in task:
                task.pop('_id')
            print('当前获取到的task为:%s'%task)
            if task['item_type'] == 'list_item':
                #这个request对象代表了一个http的请求
                #会经由downloader去执行,从而产生一个response
                yield scrapy.Request(
                    #发送请求的URL
                    url=task['task_url'],
                    dont_filter = True,
                    callback= self.handle_car_item,
                    errback= self.handle_err,
                    meta = task
                )
    #自定义的解析方法
    def handle_car_item(self,response):
        if '中为您找到0辆好车' in response.text:
            return
        #当前页面所展示的二手车,item
        car_item_list = response.xpath("//ul[@class='carlist clearfix js-top']/li")


学习愉快^_^。


0
hfloat流风
h # -*- coding: utf-8 -*- import scrapy from guazi_scrapy_project.handle_mongo import mongo from lxml import etree # parse()方法在scrapy中会直接调用,同学可以更换一个函数名称,然后参考对比下面的代码: class GuaziSpider(scrapy.Spider): name = 'guazi' allowed_domains = ['guazi.com'] # start_urls = ['http://guazi.com/'] def start_requests(self): url = 'https://www.guazi.com/anji/audi/i7/#bread' yield scrapy.Request(url=url, callback=self.handle_car_item, dont_filter=True) #自定义的解析方法 def handle_car_item(self, response): if '中为您找到0辆好车' in response.text: return #当前页面所展示的二手车,item print(response.text) car_item_list = response.xpath("//ul[@class='carlist clearfix js-top']/li") print('________________') print(car_item_list) —————————————————————————————————— 运行程序,可以打印出来response.text,但是car_item_list为空。
h020-02-20
共1条回复

好帮手乔木

2020-02-20

同学你好:

这部分代码没有问题,使用xpath可以获取到对应节点的信息,说明同学的请求路径出现问题,建议同学检查其他部分的代码。

如果我解决了同学的问题,请采纳!学习愉快^_^。

0
hfloat流风
h 正常运行没有问题,保存返回的response.text的为html,页面显示10条信息,好像数据发生较大变化。项目实战学习不下去,怎么解决。
h020-02-20
共2条回复

0 学习 · 1672 问题

查看课程