Python获取包含javascripts的web页面内容

2024-10-04 11:36:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要分析网页内容。页面有javascrips。 你能告诉我比使用硒更好的方法吗?你知道吗

如果没有: 加载到浏览器中的页面包含以下元素:

<div class="js-container">    <table class="zebra" style="width: 100%;">
        <tbody><tr>
            <th>A</th>
            <th>B</th>
            <th>C</th>
        </tr>
            <tr>
                <td>A1</td>
                <td>A2</td>
                <td>
                    <a href="http://X" style="color: black">T1</a>
                </td>
            </tr>
            <tr>
                ....
            </tr>
....

我需要逐项阅读一张表格。我跑例如:

myList = myDriver.find_elements_by_class_name("js-container")。你知道吗

那么如何获取“js container”对象的内部元素呢?你知道吗

生成myList的唯一元素是:print (myList[0])

<selenium.webdriver.remote.webelement.WebElement (session="61238", element="{71293}")>

Tags: 方法div元素stylecontainerjs浏览器页面
2条回答

也许你需要给它喂奶driver.page\u源. 它是一个python工具,可以基于web页面构建树。 BeautifulSoup document

硒可以做得很好。你知道吗

tableDescendants = myDriver.find_elements_by_css_selector("table.zebra *")
for tableDescendant in tableDescendants
    outer = tableDescendant.get_attribute("outerHTML")
    inner = tableDescendant.get_attribute("innerHTML")
    print outer[:outer.find(inner)]

此代码获取TABLE标记的所有子体,删除innerHTML字符串开头之后的所有内容并打印结果。outerHTML包含元素本身和所有子元素,innerHTML只包含子元素。因此,为了只获取元素本身的HTML,我们需要从outerHTML中删除innerHTML。你知道吗

相关问题 更多 >