from lxml import etree
import requests
if __name__ == '__main__':
url = "https://bj.58.com/ershoufang/"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/"
"85.0.4183.83 Safari/537.36"
}
page_text = requests.get(url=url, headers=headers).text
tree = etree.HTML(page_text)
li_list = tree.xpath('//ul[@class="house-list-wrap"]/li')
fp = open("./58.txt", "w", encoding="utf-8")
for li in li_list:
title = li.xpath("./div[2]/h2/a/text()")[0]
print(title)
price = li.xpath("./div[3]/p[@class='sum']/b/text()")[0]
print(price)
fp.write(title + price + "\n")
xpath 爬取某网站二手房简介及价格
最新推荐文章于 2022-10-19 13:20:02 发布