鱼C论坛

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

[已解决]xpath提取

[复制链接]
发表于 2022-7-19 18:32:14 | 显示全部楼层 |阅读模式

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

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

x
我不知道为什么在divs = 那块地方,找div[2]这个节点不存在(我截图上传不了,大家可以看看网站源代码)
大家可以打开网页帮我看看源代码吗
网站是猪八戒网,url 如下,主要想用xpath提取一下关于saas的一些公司,接单价格,所在地等基本信息,
不知道哪出了问题,可以帮我看看怎么提取吗,下面的代码到divs那我就糊涂了,谢谢大家了


  1. import requests
  2. url = "https://beijing.zbj.com/search/service/?l=0&kw=saas&r=1"
  3. resp = requests.get(url)
  4. # print(resp.text)
  5. #解析
  6. html = etree.HTML(resp.text)

  7. #拿到每个服务器的div
  8. divs = html.xpath("/html/body/div[2]")
  9. print(divs)
复制代码
最佳答案
2022-7-20 07:54:19
  1. import requests
  2. from lxml import etree

  3. url = "https://beijing.zbj.com/search/service/?l=0&kw=saas&r=1"
  4. resp = requests.get(url)
  5. # print(resp.text)
  6. # 解析
  7. html = etree.HTML(resp.text)

  8. p = []
  9. for i in range(1, 51):
  10.     name = html.xpath(
  11.         '//*[@id="__layout"]/div/div[3]/div/div[3]/div[4]/div[1]/div[{}]/a/div[2]/div[1]/div/text()'.format(
  12.             i
  13.         )
  14.     )[0]
  15.     money = html.xpath(
  16.         '//*[@id="__layout"]/div/div[3]/div/div[3]/div[4]/div[1]/div[{}]/div[3]/div[1]/span/text()'.format(
  17.             i
  18.         )
  19.     )[0]
  20.     location = (
  21.         html.xpath(
  22.             '//*[@id="__layout"]/div/div[3]/div/div[3]/div[4]/div[1]/div[{}]//div[3]/div[1]/div/text()'.format(
  23.                 i
  24.             )
  25.         )[0]
  26.         .replace("\n", "")
  27.         .replace(" ", "")
  28.     )
  29.     p.append([name, money, location])
  30. print(p)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-7-20 06:40:06 From FishC Mobile | 显示全部楼层
盲猜都是动态数据,所有诸如xpath一类的静态解析都不可以
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-7-20 07:54:19 | 显示全部楼层    本楼为最佳答案   
  1. import requests
  2. from lxml import etree

  3. url = "https://beijing.zbj.com/search/service/?l=0&kw=saas&r=1"
  4. resp = requests.get(url)
  5. # print(resp.text)
  6. # 解析
  7. html = etree.HTML(resp.text)

  8. p = []
  9. for i in range(1, 51):
  10.     name = html.xpath(
  11.         '//*[@id="__layout"]/div/div[3]/div/div[3]/div[4]/div[1]/div[{}]/a/div[2]/div[1]/div/text()'.format(
  12.             i
  13.         )
  14.     )[0]
  15.     money = html.xpath(
  16.         '//*[@id="__layout"]/div/div[3]/div/div[3]/div[4]/div[1]/div[{}]/div[3]/div[1]/span/text()'.format(
  17.             i
  18.         )
  19.     )[0]
  20.     location = (
  21.         html.xpath(
  22.             '//*[@id="__layout"]/div/div[3]/div/div[3]/div[4]/div[1]/div[{}]//div[3]/div[1]/div/text()'.format(
  23.                 i
  24.             )
  25.         )[0]
  26.         .replace("\n", "")
  27.         .replace(" ", "")
  28.     )
  29.     p.append([name, money, location])
  30. print(p)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-28 02:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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