Python 获取变化的content内容

在用Python 爬取网站信息时,碰到::before 伪元素的content内容时动态变化的。我应该如何获取content中的内容,或者定位到content中的 元素。

你应该是用selenium爬取的吧?

有时候会遇到反爬虫机制,一些元素会使用伪元素,这样在定位元素的时候会定位不到,这时候就要使用js来帮助定位,获取到想要的元素
比如html代码如下:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <title> 页面名称 </title>
<style type="text/css">
#myid:before {
    content: "abc";
}
</style>
</head>
<body>
    <div id="myid"></div>
</body>
</html>


from selenium import webdriver
import time

driver = webdriver.Chrome()

driver.get('E:/Documents/xxxxxxxx.html')
time.sleep(3)
selector = '#myid'
js_code = f'''
    return window.getComputedStyle(document.querySelector('{selector}'),':before').getPropertyValue('content').slice(1,-1);
'''
text = driver.execute_script(js_code)
print(text)

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img