weixin_40148208 2017-09-12 12:26 采纳率: 0%
浏览 1032

Python爬虫 急求大神帮忙 万分感谢

我用Python爬虫 爬取网页 爬出来获取特定数据 为什么大部分显示null 而且每次执行一次 数据就会增加一些 到底是代码问题还是网页问题 求大神指点代码下:
def get_content(self, html): #获取一个网页的内容
div_list = html.xpath("//div[contains(@class,'listtyle')]")
item_list = []
for div in div_list:
for b in range(1,19):
food_img= div.xpath("./div[@class='listtyle1'][b]/a[@class='big']/img[@class='img']/@src")
food_img=food_img[0] if len(food_img)>0 else None

            food_name = div.xpath("./div[@class='listtyle1'][b]/a[@class='big']/div[@class='i_w']/div[@class='i']/div[@class='c1']/strong/text()")
            food_name = food_name[0] if len(food_name)>0 else None
            food_effect=div.xpath("./div[@class='listtyle1'][b]/a[@class='big']/strong[@class='gx']/span/text()")
            food_effect = food_effect[0]  if len(food_effect)>0 else None
            food_time=div.xpath("./div[@class='listtyle1'][b]/a[@class='big']/div[@class='i_w']/div[@class='i']/div[@class='c2']/ul/li[@class='li1']/text()")                
            food_time = food_time[0] if len(food_time)>0 else None
            food_taste=div.xpath("./div[@class='listtyle1'][b]/a[@class='big']/div[@class='i_w']/div[@class='i']/div[@class='c2']/ul/li[@class='li2']/text()")
            food_taste = food_taste[0] if len(food_taste)>0 else None
            food_commentnum_likenum=div.xpath("./div[@class='listtyle1'][b]/a[@class='big']/div[@class='i_w']/div[@class='i']/div[@class='c1']/span/text()")
            food_commentnum_likenum = food_commentnum_likenum[0] if len(food_commentnum_likenum)>0 else None


            item=dict(

                food_img1=food_img,
                food_name1=food_name,
                food_effect1=food_effect,
                food_time1=food_time,
                food_taste1=food_taste,
                food_commentnum_likenum1=food_commentnum_likenum,
            )
            item_list.append(item)
    return item_list                
  • 写回答

2条回答

  • oyljerry 2017-09-13 01:50
    关注

    你对应的网页可能是用ajax等方式异步刷新数据的,所以你的爬虫获取的时候,数据还没有返回,就是null

    评论

报告相同问题?

悬赏问题

  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究