如何利用selenium webdriver定位网页style标签下的@规则内的内容?

如何利用selenium webdriver定位网页style标签下的@规则内的内容?

问题描述:

我正在使用selenium webdiver的方式爬取网页,代码如下,但是无法获取到如上图所示@font-face中的内容。

from selenium import webdriver

url = 'https://jn.58.com/pinpaigongyu/44466026460292x.shtml?adtype=1&from=3-list-9&slotid=1000856&productid=10043&tid=5a5efb29-2da9-428d-9519-991a2feffa7d&extParam=%7B%22ppgy_stats%22%3A%7B%22pageSource%22%3A%22%22%2C%22resource%22%3A%2258%22%2C%22abVersion%22%3A%22%22%2C%22launchid%22%3A%22%22%7D%7D&bizresource=0'


driver = webdriver.Firefox()
driver.get(url)
font_face_scripts = driver.find_element_by_xpath('/html/head/style[1]').text
print(font_face_scripts)

请问该用什么方法获取到这一内容?谢谢!

我也觉得很奇怪,元素.text无法获取到style标签中的内容。只能出此下策,在driver.page_source中查找

page_source = driver.page_source
results = re.findall(r'<style.*?</style>', page_source)
target_string = ''
for result in results:
    text = result.split('>')
    if '@font-face' in text:  # 多个style.text中筛选目标
        target_string = text
        break