【问题标题】:Print google search results using selenium in Python在 Python 中使用 selenium 打印谷歌搜索结果
【发布时间】:2017-08-17 14:16:40
【问题描述】:

我正在尝试将搜索结果打印到控制台,我在 python 语言中使用 selenium

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Chrome('/Users/Downloads/chromedriver')
browser.get('http://www.google.com')
search = browser.find_element_by_name('q')
search.send_keys("youtube")
search.send_keys(Keys.RETURN)
print(browser)
time.sleep(10)
browser.quit()

打印方式不对。

【问题讨论】:

  • 尝试 help(browser) 以获取浏览器对象中包含的适当方法和数据的列表。听起来您无法将浏览器转换为字符串。可能有另一个嵌入对象包含您想要的结果。

标签: python selenium selenium-chromedriver


【解决方案1】:

我写了一个简单的类,你可以使用,你只需要改变webdriver的路径。它是为PhantomJS 制作的(你可以下载它here。),但如果你想使用Chrome(或任何其他网络驱动程序),只需将self.driver = webdriver.PhantomJS(path) 行替换为self.driver = webdriver.Chrome(path)。下面是代码示例:

import time
from urllib.parse import quote_plus
from selenium import webdriver


class Browser:

    def __init__(self, path, initiate=True, implicit_wait_time = 10, explicit_wait_time = 2):
        self.path = path
        self.implicit_wait_time = implicit_wait_time    # http://www.aptuz.com/blog/selenium-implicit-vs-explicit-waits/
        self.explicit_wait_time = explicit_wait_time    # http://www.aptuz.com/blog/selenium-implicit-vs-explicit-waits/
        if initiate:
            self.start()
        return

    def start(self):
        self.driver = webdriver.PhantomJS(self.path)
        self.driver.implicitly_wait(self.implicit_wait_time)
        return

    def end(self):
        self.driver.quit()
        return

    def go_to_url(self, url, wait_time = None):
        if wait_time is None:
            wait_time = self.explicit_wait_time
        self.driver.get(url)
        print('[*] Fetching results from: {}'.format(url))
        time.sleep(wait_time)
        return

    def get_search_url(self, query, page_num=0, per_page=10, lang='en'):
        query = quote_plus(query)
        url = 'https://www.google.hr/search?q={}&num={}&start={}&nl={}'.format(query, per_page, page_num*per_page, lang)
        return url

    def scrape(self):
        #xpath migth change in future
        links = self.driver.find_elements_by_xpath("//h3[@class='r']/a[@href]") # searches for all links insede h3 tags with class "r"
        results = []
        for link in links:
            d = {'url': link.get_attribute('href'),
                 'title': link.text}
            results.append(d)
        return results

    def search(self, query, page_num=0, per_page=10, lang='en', wait_time = None):
        if wait_time is None:
            wait_time = self.explicit_wait_time
        url = self.get_search_url(query, page_num, per_page, lang)
        self.go_to_url(url, wait_time)
        results = self.scrape()
        return results




path = '<YOUR PATH TO PHANTOMJS>/phantomjs-2.1.1-windows/bin/phantomjs.exe' ## SET YOU PATH TO phantomjs
br = Browser(path)
results = br.search('site:facebook.com inurl:login')
for r in results:
    print(r)

br.end()

【讨论】:

    【解决方案2】:

    在java中它会像下面这样:-

    List<WebElement> print = driver.findElements(By.xpath("//div[@class='sbqs_c']"));
    System.out.println(print.size());
    for ( WebElement we: print) { 
        System.out.println(we.getText());
    }
    

    我不是 python 人,但可能会是这样:-

        browser = webdriver.Chrome('/Users/Downloads/chromedriver')
        browser.get('http://www.google.com')
        search = browser.find_element_by_name('q')
        search.send_keys("youtube")
           ids = driver.find_elements_by_xpath("//div[@class='sbqs_c']")
           for ii in ids:
           #print ii.text
           print ii.text
    

    来源:- Iterate a list with indexes in Python

    希望对你有帮助:)

    【讨论】:

      【解决方案3】:
      soup=BeautifulSoup(html)
      for link in soup.find_all('a'):
          print(link.get('href'))
      

      使用美丽的汤找到我自己问题的答案

      【讨论】:

        猜你喜欢
        • 2020-11-23
        • 2018-07-06
        • 2021-08-23
        • 1970-01-01
        • 2018-10-03
        • 1970-01-01
        • 2012-04-10
        • 1970-01-01
        • 2016-10-11
        相关资源
        最近更新 更多