鱼C论坛

 找回密码
 立即注册
查看: 1044|回复: 2

[已解决]爬虫xpath相关

[复制链接]
发表于 2020-6-3 22:30:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import requests
  2. import os
  3. import time
  4. from lxml import etree

  5. url = 'https://wuhan.esf.fang.com/'
  6. headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'}
  7. response = requests.get(url,headers=headers)
  8. result = response.text
  9. root = etree.HTML(result)

  10. title = root.xpath('//dd/p/text()')

  11. for each in title:
  12.         content = each.strip()
  13.         print(content)
复制代码

尝试写一个爬房天下武汉二手房信息,用xapth爬出房源信息中户型 面积 层高等信息,得到的是一个列表,用strip函数去掉空格后打印出来的是一条一条的信息,想把同一房源的相关信息整合到一起,如何实现?网页源代码中房源信息<p>节点中信息文本用<i>隔成一段一段的,如何处理提取整合?

  1. <p class="tel_shop">
  2.                                                                                
  3.                                                                                         3室2厅
  4.                                                                                        
  5.                                                                                                 <i>|</i>
  6.                                                                                                 89.48㎡
  7.                                                                                        
  8.                                                                                        
  9.                                                                                                 <i>|</i>
  10.                                                                                                 低层(共18层)
  11.                                                                                        
  12.                                                                                        
  13.                                                                                                 <i>|</i>
  14.                                                                                                 南北向
  15.                                                                                        
  16.                                                                                        
  17.                                                                                                 <i>|</i>
  18.                                                                                                 2009年建
  19.                                                                                        
  20.                                                                                        
  21.                                                                                                 <i>|</i>
  22.                                                                                                 <span class="people_name">
复制代码
最佳答案
2020-6-4 07:33:02
  1. import requests
  2. import os
  3. import time
  4. from lxml import etree

  5. url = 'https://wuhan.esf.fang.com/'
  6. headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'}
  7. response = requests.get(url,headers=headers)
  8. result = response.text
  9. result = result.replace('<i>|</i>','')
  10. root = etree.HTML(result)

  11. title = root.xpath('//p/[@class="tel_shop"]')

  12. for each in title:
  13.         content = each.strip()
  14.         print(content)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-6-4 07:33:02 | 显示全部楼层    本楼为最佳答案   
  1. import requests
  2. import os
  3. import time
  4. from lxml import etree

  5. url = 'https://wuhan.esf.fang.com/'
  6. headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'}
  7. response = requests.get(url,headers=headers)
  8. result = response.text
  9. result = result.replace('<i>|</i>','')
  10. root = etree.HTML(result)

  11. title = root.xpath('//p/[@class="tel_shop"]')

  12. for each in title:
  13.         content = each.strip()
  14.         print(content)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-5 19:43:36 | 显示全部楼层
虽然没有达到我想要的效果,可能是我自己没有表达清楚。但是你这个建议提供了一个很好的思路,就是得到html源代码后可以先对代码文本进行预处理,把奇怪的格式简化。感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-21 10:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表