【问题标题】:Python -Google Search - How to set flexible results pickingPython -Google 搜索 - 如何设置灵活的结果选择
【发布时间】:2019-04-28 11:59:11
【问题描述】:

我正在尝试抓取一些通过 Google 搜索访问它们的页面,并且我需要添加一些受限字词列表。

假设 Python 在 Google 搜索中排名靠前的 4 个结果是:

然后我想打开第一个不包含以下单词的结果: [".org", "wikipedia"] 在搜索描述和/或链接中-(因此在这种情况下脚本将打开 w3schools)

我试图用不同的选择器完成工作/并获得整个谷歌搜索页面文档,但到目前为止没有得到积极的结果:

search = driver.find_element_by_name('q') 
search.send_keys("Gran Hotel La Florida G.L Monumento")
search.send_keys(Keys.RETURN) # hit return after you enter search text time.sleep(5)
driver.find_element_by_class_name('LC20lb').click()

这会打开第一个非广告结果。

【问题讨论】:

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


    【解决方案1】:

    CSS:

    可能类似于以下内容,基于href 排除(也仅限于以http 开头的href 并删除具有.fl 类的那些。:not 伪类传递了一个条件列表-在这种情况下主要是子字符串通过包含运算符排除。

    .r [href^=http]:not(.fl,[href*=\.org],[href*=wikipedia])
    

    下面的测试用例在多个不同国家/地区的 Google 搜索中进行了测试

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from bs4 import BeautifulSoup as bs
    
    d = webdriver.Chrome()
    d.get('https://www.google.com/')
    d.find_element_by_css_selector('[title=Search]').send_keys('python')
    WebDriverWait(d, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[type=submit]'))).click()
    WebDriverWait(d,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,'.r')))
    soup = bs(d.page_source, 'lxml')
    links =  [link['href'] for link in soup.select('.r [href^=http]:not(.fl,[href*=\.org],[href*=wikipedia])')]
    print(links)
    

    【讨论】:

    • 始终使用 EC.element 进行 clicable 我得到 selenium.common.exceptions.TimeoutException:消息:
    • 我认为是因为您的本地 google 搜索会有不同的文本字符串。对于 Google 搜索按钮(当您输入搜索词时出现在底部的两个按钮的左侧)——您需要检查元素并查看 value 属性的值在什么地方被调用你的语言。在英语中,它表示 Google Search。
    • 您也可以使用 [type="submit"] 作为选择器而不是 [value="Google Search"]
    • 我已经更新了适用于各国的版本。
    【解决方案2】:

    您可以更新您的选择器以点击所需的链接:

    driver.find_element_by_xpath('//h3[@class="LC20lb" and not(contains(text(), "org")) and not(contains(text(), "wikipedia"))]').click()
    

    这将排除包含子字符串 "org""wikipedia" 的结果

    【讨论】:

    • 我真的希望有这样的东西:selenium.common.exceptions.InvalidSelectorException:消息:无效选择器:无法使用 xpath 表达式定位元素 //h3[@class="LC20lb" |不(包含(文本(),“预订”))| not(contain(text(), "tripadvisor"))] 因为以下错误:SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//h3[@class="LC20lb" and not(contain (text(), "org")) 而不是(contain(text(), "wikipedia"))]').click() 但是: driver.find_element_by_xpath('//h3[@class="LC20lb"]' ).click() 工作正常
    • @ArturGorczyński ,是的,我的错 :) 将 contain 替换为 contains
    • 与“包含”相同的错误:SyntaxError: 无法在“文档”上执行“评估”:字符串 '//h3[@class="LC20lb"] 而不是 (contains(text( ), "org")' 不是有效的 XPath 表达式。:
    • @ArturGorczyński 不要在第一个谓词之后关闭括号:注意你使用了[@class="LC20lb"] and...,但它应该是[@class="LC20lb" and...
    • 噢,我很笨:|仍在努力解决一些小问题,但它正在工作;)谢谢伙计!与此同时,我发现类似这样的东西“driver.find_element_by_xpath(("//*[not(contains(text(), 'org'))][@class='LC20lb']")).click()”也工作得很好。再次感谢!!!
    猜你喜欢
    • 2016-03-15
    • 2019-06-06
    • 2018-07-19
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-31
    相关资源
    最近更新 更多