


xpath:'//*[@id="side_right"]/div[3]';
xpath:'//*[@id="side_right"]/div[4]';get_attribute('outerHTML')方法进行这两个元素的outerHTML获取:# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2022/10/13
# 文件名称:test_selenium_otherHTML.py
# 作用:xxx
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
from selenium import webdriver
import time
content_list = ["content_48_h", "content_10_d"]
el_xpath = ['//*[@id="side_right"]/div[3]',
'//*[@id="side_right"]/div[4]']
content = []
driver = webdriver.Chrome()
driver.get("https://www.cnblogs.com/")
time.sleep(2)
for i in range(0, 2):
content_list[i] = driver.find_element_by_xpath(el_xpath[i])
content.append(content_list[i].get_attribute('outerHTML'))
print(f"48小时阅读排行为:{content[0]}",
f"10天推荐排行为:{content[1]}")
time.sleep(2)
driver.quit()# 48小时阅读排行
'//*[@id="side_right"]/div[3]'
# 10天推荐排行
'//*[@id="side_right"]/div[4]'requests的get方法进入网站:res = requests.get('https://www.cnblogs.com/',
verify=False,
headers=headers)etree方法解析:tree = etree.HTML(res.content)tree.xpath('//*[@id="side_right"]/div[3]')
tree.xpath('//*[@id="side_right"]/div[4]')from lxml import etree
import requests
content_list = ["content_48_h", "content_10_d"]
el_xpath = ['//*[@id="side_right"]/div[3]',
'//*[@id="side_right"]/div[4]']
content = []
headers = {'Connection': 'close'}
res = requests.get('https://www.cnblogs.com/', verify=False, headers=headers)
tree = etree.HTML(res.content)
for i in range(0, 2):
content_list[i] = tree.xpath(el_xpath[i])
print(content_list[i])
content.append(etree.tostring(content_list[i][0], encoding='utf-8'))
print(f"48小时阅读排行为:{content[0]},",
f"10天推荐排行为:{content[1]}") File "F:\python_study\test_selenium_otherHTML.py", line 24, in <module>
content.append(etree.tostring(content_list[i][0], encoding='utf-8'))
IndexError: list index out of range
[]xpath页面的内容为空,那么可以猜测是因为这个https://www.cnblogs.com/下没有对应的'//*[@id="side_right"]/div[3]'或'//*[@id="side_right"]/div[4]'https://www.cnblogs.com/下的源码,进行查找我们的关键字【48小时阅读排行】和【10天推荐排行】:

https://www.cnblogs.com/下没有对应的'//*[@id="side_right"]/div[3]'或'//*[@id="side_right"]/div[4]',换言之,我们需要的元素不在这个页面,虽然我们但从网页看是在同一页面,但可能是其他页面加载出来的。所以我们得找到这个原色所在的页面,重新进行定位。https://www.cnblogs.com/;https://www.cnblogs.com/aggsite/SideRight后,发现返回值里边有我们需要的关键字,那么这个接口地址才是我们需要的,而不是https://www.cnblogs.com/;
https://www.cnblogs.com/aggsite/SideRight的返回值到vscode中,并进行运行:

https://www.cnblogs.com/aggsite/SideRight;xpath)。如下:
# 48小时阅读排行
'/html/body/div[1]/ul',
# 10天推荐排行
'/html/body/div[2]/ul'from lxml import etree
import requests
content_list = ["content_48_h", "content_10_d"]
el_xpath = ['/html/body/div[1]/ul',
'/html/body/div[2]/ul']
content = []
headers = {'Connection': 'close'}
res = requests.get('https://www.cnblogs.com/aggsite/SideRight', verify=False, headers=headers)
tree = etree.HTML(res.content)
for i in range(0, 2):
content_list[i] = tree.xpath(el_xpath[i])
print(content_list[i])
content.append(etree.tostring(content_list[i][0], encoding='utf-8'))
print(f"48小时阅读排行为:{content[0]},",
f"10天推荐排行为:{content[1]}")原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。