鱼C论坛

 找回密码
 立即注册
查看: 5110|回复: 1

定位不了需要的属性,用selenium得到的html和网页看的源码也不一样

[复制链接]
发表于 2022-5-4 23:51:57 | 显示全部楼层 |阅读模式

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

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

x
  1. # usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. #需求爬取该代码中url对应网页中每个黄花菜资料的详情文本和图片

  4. import  requests
  5. from lxml import  etree
  6. import os

  7. if not os.path.exists('./黄花'):
  8.     os.mkdir('./黄花')

  9. url  = 'http://www.qdtcp.com/story/huanghuacai/'
  10. headers = {
  11.         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 Edg/100.0.1185.50'
  12.     }


  13. html = requests.get(url=url,headers=headers).text.encode('utf-8')
  14. tree = etree.HTML(html)

  15. div_list = tree.xpath('//*[@id="wp"]/div[3]/div[1]/div[2]/div[2]/div')
  16. print(div_list)
  17. for div in div_list:
  18.     href = div.xpath('./div[1]/dl/dt/a/@href')[0]
  19.     print(href)

  20. xpath表达式得到的列表是空的
  21. 试过用selenium,但是page_source得到的网页 和浏览器上面的源码不同

  22. 蹲一个大佬解惑
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-5-5 00:24:27 | 显示全部楼层
靠,我把那个.encode('utf-8') 去掉就好了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-24 09:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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