爬虫实战-房天下(bengbu.zu.fang.com/)数据爬取

详细代码链接https://flowus.cn/hbzx/3c42674d-8e6f-42e3-a3f6-bc1258034676

import requests

from lxml import etree #xpath解析库

def 源代码(url):

cookies = {

'global_cookie': 'xeqnmumh38dvpj96uzseftwdr20lvkwkfb9',

'otherid': 'b44a1837638234f1a0a15e37877e0685',

'g_sourcepage': 'zf_fy%5Elb_pc',

'__utma': '147393320.818863681.1714391725.1714391725.1714391725.1',

'__utmc': '147393320',

'__utmz': '147393320.1714391725.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)',

'__utmt_t0': '1',

'__utmt_t1': '1',

'__utmt_t2': '1',

'keyWord_recenthousebengbu': '%5b%7b%22name%22%3a%22%e9%be%99%e5%ad%90%e6%b9%96%22%2c%22detailName%22%3a%22%22%2c%22url%22%3a%22%2fhouse-a011914%2fs31%2f%22%2c%22sort%22%3a1%7d%5d',

'city': 'sh',

'ASP.NET_SessionId': '4fpr5u3w5zqqzitrnwafk3cr',

'zf_csrfcookie': '1cCMHJcsaY7XgtGVMdiMdsydBeGKPxx7G1pYnsK0yn4vRI361O_aeBQfC7SAKi4gktL0kQ2',

'unique_cookie': 'U_xeqnmumh38dvpj96uzseftwdr20lvkwkfb9*6',

'__utmb': '147393320.18.10.1714391725',

}

headers = {

'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',

'accept-language': 'zh-CN,zh;q=0.9',

'cache-control': 'max-age=0',

'cookie': 'global_cookie=xeqnmumh38dvpj96uzseftwdr20lvkwkfb9; otherid=b44a1837638234f1a0a15e37877e0685; g_sourcepage=zf_fy%5Elb_pc; __utma=147393320.818863681.1714391725.1714391725.1714391725.1; __utmc=147393320; __utmz=147393320.1714391725.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmt_t0=1; __utmt_t1=1; __utmt_t2=1; keyWord_recenthousebengbu=%5b%7b%22name%22%3a%22%e9%be%99%e5%ad%90%e6%b9%96%22%2c%22detailName%22%3a%22%22%2c%22url%22%3a%22%2fhouse-a011914%2fs31%2f%22%2c%22sort%22%3a1%7d%5d; city=sh; ASP.NET_SessionId=4fpr5u3w5zqqzitrnwafk3cr; zf_csrfcookie=1cCMHJcsaY7XgtGVMdiMdsydBeGKPxx7G1pYnsK0yn4vRI361O_aeBQfC7SAKi4gktL0kQ2; unique_cookie=U_xeqnmumh38dvpj96uzseftwdr20lvkwkfb9*6; __utmb=147393320.18.10.1714391725',

'priority': 'u=0, i',

'referer': 'https://sh.zu.fang.com/house/i33/',

'sec-ch-ua': '"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',

'sec-ch-ua-mobile': '?0',

'sec-ch-ua-platform': '"Windows"',

'sec-fetch-dest': 'document',

'sec-fetch-mode': 'navigate',

'sec-fetch-site': 'same-origin',

'sec-fetch-user': '?1',

'upgrade-insecure-requests': '1',

'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',

}

response = requests.get(url, cookies=cookies, headers=headers).text

#response是源代码

return response

if name=='main':

for fan in range(1,10):

url='https://sh.zu.fang.com/house/i3{fan}/'

res=源代码(url)

res=etree.HTML(res) #初始化

#//*@id="rentid_D09_01_02"/a a后面没有加/text()

#//*@id="rentid_D09_60_02"/a

#//*@id="rentid_D09_02_02"/a

#//*@id="rentid_D09_31_02"/a

for i in range(1,61):

if i<10:

ix='0'+str(i)

xp=f'//*@id="rentid_D09_{ix}_02"/a/text()'

else:

xp=f'//*@id="rentid_D09_{i}_02"/a/text()'

title=res.xpath(xp)0

print(title,end=' ')

xp=f'//*@id="listBox"/div3/dl{i}/dd/p2/text()'

#//*@id="listBox"/div3/dl60/dd/p2

p=res.xpath(xp)0.strip()

print(p,end=' ')

xp=f'//*@id="listBox"/div3/dl{i}/dd/div2/p/span/text()'

#//*@id="listBox"/div3/dl60/dd/div2/p/span

jg=res.xpath(xp)0

print(jg)

相关推荐
IT毕设实战小研11 小时前
基于大数据的WTA职业网球赛事演变与竞技格局数据可视化分析
大数据·后端·爬虫·python·信息可视化·课程设计
2601_9623824312 小时前
用“爬虫”抓取小红书内容侵权吗 福建法院判明边界
爬虫·数字经济·知识产权·不正当竞争·福建法院
Mycdn_WD12 小时前
从 AI 爬虫变多了到 AI 抓取开始分流,CDN 行业进入下一阶段
人工智能·爬虫·cdn·pcdn·城域网·pcdn资源招募
该逃避避1 天前
爬虫代理配置指南:从0搭建Crawl4AI网页爬虫教程
爬虫
TechWayfarer1 天前
Cloudflare 9·15新政倒计时:用IP风险画像识别AI爬虫,防止误伤Googlebot
网络·人工智能·爬虫·python·tcp/ip·网络安全
susplus1 天前
【HTTP协议】HTTP介绍及基础【C语言爬虫实现】
c语言·爬虫·http·万维网
绘梨衣5472 天前
AI技术栈全景指南_Prompt_RAG_爬虫_MCP
人工智能·爬虫·prompt
stolentime2 天前
OpenClaw 2.0 新手快速上手与实战指南
爬虫·python·ai·网络爬虫
玫瑰互动GEO2 天前
工程视角看GEO优化与SEO优化:爬虫排序 vs 大模型引用
人工智能·爬虫·搜索引擎
for_ever_love__2 天前
python爬虫: 数据解析
开发语言·爬虫·python·xpath