import requests
from lxml import etree
url = 'https://changsha.zbj.com/xcxkfzbjzbj/f.html?fr=zbj.sy.zyyw_2nd.lv3&r=2'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.49'
}
resp = requests.get(url=url,headers=headers)
# print(resp.text)
# 解析
tree = etree.HTML(resp.text)
# 定位到一个大框架,里面放着每一个商品的标题,价格什么的
divs = tree.xpath('//*[@class="search-result-list"]/div[1]/*')
# print(divs)
for div in divs:
title = div.xpath('.//*[@class="name-pic-box"]/a/text()')
print(title)
标签:url,text,resp,猪八戒,爬取,服务商,print,div,divs
From: https://www.cnblogs.com/Wesuiliye/p/17174044.html