简体   繁体   English

PYTHON:在 html 源代码中查找隐藏元素

[英]PYTHON : Finding hidden element in html source code

I'm trying get the url below which is hidden inside the page source code at https://www.aliexpress.com/item/32212764152.html but it's hidden inside a script tag.我正在尝试获取下面的 url,它隐藏在https://www.aliexpress.com/item/32212764152.ZFC35FDC70D5FC69D2698883A8的页面源代码中

<script>
                    window.runParams = {"descriptionModule":{"descriptionUrl":"https://aeproductsourcesite.alicdn.com/product/description/pc/v2/en_US/desc.htm?productId=32212764152&key=HTB1GwO_aVY7gK0jSZKzM7OikpXac.zip&token=f32528ddd34e37aecddda1c7778d5f0c"} .... </script>

I've manage to get the source code but not sure how to extract the url as an object.我已经设法获得源代码,但不确定如何将 url 提取为 object。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
import re

options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument("--test-type")
CHROMEDRIVER_PATH = '/Users/reezalaq/PycharmProjects/wholesale/driver/chromedriver'
options = Options()
options.headless = False
driver = webdriver.Chrome(CHROMEDRIVER_PATH, options=options)
driver.get('https://www.aliexpress.com/item/32212764152.html')

html = driver.page_source

def run_script():
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    body = driver.find_element_by_css_selector('body')
    body.send_keys(Keys.PAGE_UP)

count = 0
while count < 3: #13
     run_script()
     count+=1
     time.sleep(5)

x = html.startswith('https://aeproductsourcesite.alicdn.com')
print(x)

How do I filter everything else in the source code and have an object?如何过滤源代码中的所有其他内容并拥有 object?

x = " https://aeproductsourcesite.alicdn.com/product/description/pc/v2/en_US/desc.htm?productId=32212764152&key=HTB1GwO_aVY7gK0jSZKzM7OikpXac.zip&token=f32528ddd34e37aecddda1c7778d5f0c " x = " https://aeproductsourcesite.alicdn.com/product/description/pc/v2/en_US/desc.htm?productId=32212764152&key=HTB1GwO_aVY7gK0jSZKzM7OikpXac.zip&token35a7cddda18ddd784 "

You can use Regular Expression to extract the value:您可以使用正则表达式来提取值:

import re
#..
url = re.compile(r'"descriptionUrl":"([^"]*)"').search(html).group(1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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