简体   繁体   中英

How to get the value within a script that has no class name?

I am trying to scrape information from a website using Selenium.

A snippet of the html code is below:

<script>
  window.RI.storeId = "1";    
</script>

How can I get the "1" value despite there not being a class name?

Use the find_elements_by_tag_name() then iterate and check the element.get_attribute("textContent") contains window.RI.storeId and then do some string manipulation.you can use some regex as well.

elements=driver.find_elements_by_tag_name("script")

for element in elements:
    if "window.RI.storeId" in element.get_attribute("textContent"):
        scripttext=element.get_attribute("textContent")
        break

print(scripttext.split('"')[1])

If you want to use regex you need to import re.

import re

elements=driver.find_elements_by_tag_name("script")

for element in elements:
    if "window.RI.storeId" in element.get_attribute("textContent"):
        scripttext=element.get_attribute("textContent")
        break
print(re.findall("[window.RI.storeId\s=\s\"]\d{1}",scripttext)[0].split('"')[-1])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM