0

不幸的是,我的 Python 程序的实现有一点问题。在某一时刻,我无法再进一步了。该程序应执行以下操作:

  1. 在搜索引擎“www.startpage.com”上执行特定关键字的自动搜索。
  2. 然后应该读出带有结果的页面(这就是问题所在)。
  3. 该程序现在应该计算某个单词在搜索结果页面上出现的频率。

这里的问题是我无法从搜索结果页面获取源代码。我只得到起始页的源代码有人知道解决方案吗?

提前致谢。

到目前为止,我的程序如下所示:

import selenium.webdriver as webdriver

def get_results(search_term):

    #this is the site, where I want to do the search
    url="https://www.startpage.com"
    browser = webdriver.Firefox()
    browser.get(url)

    search_box = browser.find_element_by_id("q")
    #search in the search box after the search term
    search_box.send_keys(search_term)
    search_box.submit()

    #print(browser.page_source) would give the result of the startpage (not the result page)

    sub="dog"
    print(source_code.count("dog"))
    #counts zero times because it searchs for "dog" at the startpage

get_results("dog")

4

1 回答 1

1

你可以这样做:只要创建一个循环,当找到这个词时,你总是将一个元素添加到列表中(例如,可以是数字或字母)。

为此,您必须将源代码保存在变量中,然后简单地在其中搜索术语。当它被发现时,您只需将一个数字添加到列表中.append(),然后在最后使用 . 检查列表的长度len(list)

于 2020-10-21T13:02:02.420 回答