【问题标题】:Unable to Extract JavaScript Elements while Scraping Quora抓取 Quora 时无法提取 JavaScript 元素
【发布时间】:2019-01-30 07:01:54
【问题描述】:

我正在尝试使用 Python、BeautifulSoup 和 Selenium 从 Quora 中提取数据以进行分析。但我无法提取页面上的 JavaScript 元素。我应该如何提取它们?

这里我只是想提取 Quora 个人资料的简历,但我没有收到点击“更多”按钮后出现的文本。

~https://imgur.com/a/fTmeh1m

                # Extracting Bio
                driver.find_element_by_class_name('ui_qtext_more_link').send_keys(Keys.ENTER)
                bio = driver.find_element_by_class_name("ui_qtext_rendered_qtext").text

【问题讨论】:

    标签: python-3.x selenium-webdriver beautifulsoup web-crawler quora


    【解决方案1】:

    请使用下面的代码行首先点击“更多”按钮,然后获取配置文件的展开文本。

    import time
    //Fetch the more button element first
    WebElement moreButton = driver.find_element_by_xpath("(//a[@class='ui_qtext_more_link'])[1]");    
    //Click on the more button
    moreButton.click();
    time.sleep(3)
    //Fetch the profileInfo element
    WebElement profileInfo = driver.find_element_by_xpath("(//div[contains(@id,'expanded_content')]//span[@class='ui_qtext_rendered_qtext'])[1]");
    //Store the bio in a string and use it further
    String profileInfoBio = profileInfo.text;
    

    【讨论】:

    • 我在 6 个不同的配置文件上使用了上面的代码,它只提取了其中 2 个的 Bio。对于其他人来说,输出是生物中没有的其他东西。
    • 你能给我代码不起作用的配置文件名称吗?
    • 我已经编辑了代码,请现在检查并在点击每个不同的配置文件后获取新元素。让我知道它是否有效
    • 是的,它现在可以工作了。您唯一更改的是添加延迟。那么它为什么适用于某些配置文件但不适用于其他配置文件。另外,我对我的代码做了类似的事情,但是使用“find_element_by_class_name”,你使用了 Xpath。这两种方法有什么区别,能告诉我吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多