简体   繁体   English

使用 Python 中的 Selenium 从“结果页面”中提取结果

[英]Extracting Results from “Result-Page” With Selenium in Python

Unfortunately I have a little problem with the realization of my Python program.不幸的是,我的 Python 程序的实现有点问题。 At one point I can't get any further.在某一时刻,我无法再进一步了。 The program should do the following:该程序应执行以下操作:

  1. Perform an automatic search for a specific keyword on the search engine "www.startpage.com".在搜索引擎“www.startpage.com”上执行特定关键字的自动搜索。
  2. Then the page with the results should be read out (and here is the problem).然后应该读出带有结果的页面(这就是问题所在)。
  3. The program should now count how often a certain word appears on the page with the search results.该程序现在应该计算某个单词在搜索结果页面上出现的频率。

The problem here is that I can't get the source code from the search results page.这里的问题是我无法从搜索结果页面获取源代码。 I only get the source code of the start page Does anyone know a solution?我只得到起始页的源代码有人知道解决方案吗?

Thanks in advance.提前致谢。

So far my program looks like this:到目前为止,我的程序如下所示:

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")

You can do it this way: Just make a loop in which you always add an elment to a list (can be a number or a letter, for example) when this term is found.你可以这样做:只要创建一个循环,当找到这个词时,你总是将一个元素添加到列表中(例如,可以是数字或字母)。

To do this you have to save the source code in a variable and then simply search for the term in it.为此,您必须将源代码保存在变量中,然后简单地在其中搜索术语。 When it is found you simply add a number to a list with .append() and then at the end you check the length of the list with len(list) .找到后,您只需使用.append()将一个数字添加到列表中,然后在最后使用len(list)检查列表的长度。

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

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