import requests
from lxml import etree
import csv
url=('https://www.58.com/fangjiawang/sheng-2023-27/')
myheader={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 Edg/109.0.1518.70"}
response = requests.get(url,headers=myheader)
allList = []
res = response.content.decode()
myhtml=etree.HTML(res)
divList = myhtml.xpath('//div[@class="price-item"]//text()',ecoding='utf-8')
#print(divList)
for itemHtml in divList:
cs = itemHtml.xpath('./ul/li/a/b/text()')
fj = itemHtml.xpath('./ul/li/a/span/text()')
zzl = itemHtml.xpath('./ul/li/a/em/text()')
print(cs)
Traceback (most recent call last):
File "C:\Users\ASUS\Desktop\房价.py", line 14, in <module>
cs = itemHtml.xpath('./ul/li/a/b')
AttributeError: 'lxml.etree._ElementUnicodeResult' object has no attribute 'xpath'