亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 google 中迭代搜索一本書名,然后單擊 amazon.com 中的鏈接

在 google 中迭代搜索一本書名,然后單擊 amazon.com 中的鏈接

青春有我 2023-10-11 22:55:29
我正在使用以下腳本from selenium import webdriverimport timeimport urllib.parsebrowser = webdriver.Chrome()with open("google-search-terms.adoc") as fin:    for line_no, line in enumerate(fin):        line = line.strip()        query = urllib.parse.urlencode({'q': line})        browser.execute_script(            f"window.open('https://www.google.com/search?{query}');")for x in range(len(browser.window_handles)):    browser.switch_to.window(browser.window_handles[x])    time.sleep(3)    try:        browser.find_elements_by_xpath(            "//*[@id='rso']/div/div/div/a/div/cite[contains(text(),'amazon')]").click()    except:        pass輸入文件google-search-terms.adoc包含:The Effective Executive by Peter DruckerThe Functions of the Executive它打開多個選項卡,其中包含輸入文件中文本的搜索結果。它每 3 秒循環一次選項卡。然而點擊的不是預期的搜索結果?這里有什么問題嗎?
查看完整描述

1 回答

?
慕容森

TA貢獻1853條經驗 獲得超18個贊

谷歌有一項功能,您可以從特定網站獲取結果。因此,這里的過程只是通過該功能進行搜索并單擊找到的第一個鏈接:


from selenium import webdriver

import time

import urllib.parse


browser = webdriver.Chrome()


with open("google-search-terms.adoc") as fin:

    for line_no, line in enumerate(fin):

        line = line.strip()

        query = urllib.parse.urlencode({'q': line + " site:amazon.com"})

        browser.execute_script(

            f"window.open('https://www.google.com/search?{query}');")


for x in range(len(browser.window_handles)):

    browser.switch_to.window(browser.window_handles[x])

    time.sleep(2)

    try:

        result = browser.find_elements_by_xpath('//div[@id="rso"]/div/div')[0] 

        result.find_element_by_xpath("./div/a").click()

    except:

        continue


查看完整回答
反對 回復 2023-10-11
  • 1 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號