本帖最后由 ZY.Zhang 于 2021-5-15 11:46 编辑
[Python] 纯文本查看 复制代码 import requests
from lxml import etree
if __name__ == '__main__':
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
}
url = 'https://cs.zu.fang.com/'
page_text = requests.get(url = url,headers = headers).text
tree = etree.HTML(page_text)
dl_list = tree.xpath('//*[@id="listBox"]/div[2]/dl')
fp = open('ershoufang.txt','w',encoding = 'utf-8')
for dl in dl_list:
title = dl.xpath('./dd/p[1]/a/text()')[0]
fp.write(title + '\n\n')
print('---------------Over!------------------')
继续想获取每个房源的具体信息的话,直接取a标签的href属性,前边补充链接即可 |