【问题标题】:Unable to scrape some text out of a hidden container无法从隐藏的容器中抓取一些文本
【发布时间】:2019-02-19 08:55:56
【问题描述】:

我已经在 python 中编写了一个脚本来抓取位于类 floorplan 中的某个文本,该类在 right-column 内,又在 modal-body 内。但是,当我运行我的脚本时,它会给出空白输出?

link to that site

点击前的元素(floorplanswing 类中的值为 null):

<div class="right-column">
    <div class="field" ng-show="selectedLot.Name !== ''">
        <div class="label">Home Design:</div>
        <div class="floorplan value ng-binding"></div>
        <hr>
    </div>
    <div class="field" ng-show="selectedLot.ShortDescription !== ''">
        <div class="label">Elevation:</div>
        <div class="swing value ng-binding"></div>
        <hr>
    </div>
    <div class="field" ng-show="selectedLot.Swing !== ''">
        <div class="label">Swing:</div>
        <div class="swing value ng-binding"></div>
        <hr>
    </div>
</div>

点击后(值现在存在于floorplanswing 类中):

<div class="right-column">
    <div class="field" ng-show="selectedLot.Name !== ''">
        <div class="label">Home Design:</div>
        <div class="floorplan value ng-binding">Delaware</div>
        <hr>
    </div>
    <div class="field" ng-show="selectedLot.ShortDescription !== ''">
        <div class="label">Elevation:</div>
        <div class="swing value ng-binding">TRA</div>
        <hr>
    </div>
    <div class="field" ng-show="selectedLot.Swing !== ''">
        <div class="label">Swing:</div>
        <div class="swing value ng-binding">Garage Right</div>
        <hr>
    </div>
</div>

到目前为止,我已经尝试过 (can't make my script click on that image to reveal the data I'm after):

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def collect_links(link):
    driver.get(link)
    wait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR,"path#ip-loader-circle")))
    item = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,".modal-body .right-column .floorplan")))
    print(item.get_attribute("innerHTML"))

if __name__ == '__main__':
    url = "https://khovsecure.ml3ds-cloud.com/index.html?_ga=2.181197287.1174152084.1550480313-902396065.1550480313#/lotmap/43935"
    driver = webdriver.Chrome()
    wait = WebDriverWait(driver,20)
    collect_links(url)
    driver.quit()

预期输出:

Delaware

当在该地图上发起点击时,信息在框中弹出的方式如下:

如何在该地图上单击以从弹出容器中抓取所需的文本?

【问题讨论】:

    标签: python python-3.x selenium selenium-webdriver web-scraping


    【解决方案1】:

    以下代码为您提供 json 格式的所有数据:

    import requests
    
    if __name__ == '__main__':
        headers = {
            'fullurl': 'https://khovsecure.ml3ds-cloud.com/index.html?_ga=2.181197287.1174152084.1550480313-902396065.1550480313#/lotmap/43935',
        }
        response = requests.get('https://khovsecure.ml3ds-cloud.com/resources/data/CommunityData/khovsecure.ml3ds-cloud.com', headers=headers)
        print(response.json())
    

    【讨论】:

    • 尝试您建议的脚本时出现超时异常错误。仅当在地图上执行单击时才会出现这些元素。谢谢。
    • @MITHU 检查我的答案更新。不需要selenium来获取数据,你可以使用requests来获取所有的json然后解析它。
    • 是的,它看起来更好@Sers。你在哪里找到这个链接?我尝试在 Allxhr 选项卡中使用 chrome 开发工具获取相同的链接,但我找不到相同的链接?可以建议我的方式吗?谢谢。
    • 在devtools进入network tab刷新页面,就可以找到链接了。
    猜你喜欢
    • 2020-11-05
    • 2016-08-26
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    • 2019-09-23
    相关资源
    最近更新 更多