coding:utf8作者:wangxingchun日期:2022年03月07日importrequestsfromlxmlimportetreeurlhttps:sh。58。comershoufangheaders{useragent:Mozilla5。0(MIntelMacOSX10157)AppleWebKit537。36(KHTML,likeGecko)Chrome98。0。4758。109Safari537。36}cookies{cookie:}resprequests。get(url,headersheaders,cookiescookies)print(resp。text)创建etree对象treeetree。HTML(resp。text)使用定位58二手房内容,注意这里用的是属性定位,取到该页面的所有信息列表resultlisttree。xpath(p〔classpropertycontentdetail〕)遍历以上列表forcontentinresultlist:titlecontent。xpath(。p〔1〕h3text())相对路径,获取需要的数据信息。print(title)