扶荔 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 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算