扶荔 2019-04-26 12:35 采纳率: 0%
浏览 8666

为啥爬虫运行成功但是没有输出数据

import requests
from lxml import etree

获取页面源码数据

url = 'https://su.58.com/ershoufang/?utm_source=market&spm=u-2d2yxv86y3v43nkddh1.BDPCPZ_BT&PGTID=0d100000-0000-5181-2fa5-ee4c1abecbb9&ClickID=3' \

headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.6821.400 QQBrowser/10.3.3040.400'

}
page_text = requests.get(url=url,headers=headers).text

实例化etree对象并且将页面源码数据加载到对象中

tree = etree.HTML(page_text)
li_list = tree.xpath('//ul[@class="huose-list-wrap"]/li')
all_data_list = []
for li in li_list:
title = li.xpath('.//div[@class="list-info"]/h2/a/text()')[0]
detail_url = li.xpath('.//div[@class="list-info"]/h2/a/@href')[0]
if not 'https:' in detail_url:
detail_url = 'https:'+detail_url
price = li.xpath('.//ul[@class="price"]/p//text()')
price = ''.join(price)

# 对详情页发起请求,获取页面数据
detail_page_text = requests.get(url=detail_url,headers=headers).text
tree = etree.HTML(detail_page_text)
desc = tree.xpath('.//div[@class="general-item-wrap""]//text()')
desc = ''.join(desc).strip("\n\b\t")

dic = {
    'title':title,
    'price':price,
    'desc':desc
}

all_data_list.append(dic)

print(all_data_list)

  • 写回答

1条回答 默认 最新

  • 睡觉觉哦 2019-04-26 13:17
    关注

    把detail_page_text,title,price,desc打印出来,看看问题出在哪

    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用