利用xpath方法抓取的列表为空
需求:抓取房源的标题,地址,户型,价格
网址:https://changde.58.com/xinfang/?PGTID=0d100000-0036-8bd3-6159-08ef6dac6e41&ClickID=4
有谁可帮我看一下 嘛
# 需求:爬取58同城上常德新房的房源信息
import requests
from lxml import etree
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0'
}
# 抓取页面源码数据
url = 'https://changde.58.com/xinfang/?PGTID=0d100000-0036-8511-a0ff-530399c9a35a&ClickID=2'
page_text = requests.get(url=url, headers=headers).text
# 数据解析
tree = etree.HTML(page_text)
div_list = tree.xpath('//div[@class="key-list imglazyload"]/div')
f1 = open('./changDe fangYuan.txt', 'w', encoding='utf-8')
for div in div_list:
# 局部解析
items_name = div.xpath('./div/a[1]/span/text()')[0]
address = div.xpath('./div/a[2]/span/text()')[0]
HuXing = div.xpath('./div/a[3]/span/text()')[0]
price = div.xpath('./a[2]/p/span/text()')[0]
f1.write(items_name+address+HuXing+price)
print('打印成功!')