我計劃構建一個同時使用 Selenium 和 BeautifulSoup 的抓取工具。我正在努力單擊帶有硒的加載更多按鈕。我已經設法檢測到按鈕,滾動到它等等 - 似乎無法找到連續點擊按鈕的方法。關于如何通過這個障礙有什么建議嗎?from selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom selenium.common.exceptions import TimeoutException, NoSuchElementExceptionfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECimport time, requestsfrom bs4 import BeautifulSoupdef search_agent(zip): location = bot.find_element_by_name('hheroquotezip') time.sleep(3) location.clear() location.send_keys(zip) location.submit()def load_all_agents(): # click more until no more results to load while True: try: #more_button = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'results.length'))).click() more_button = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="searchResults"]/div[3]/button'))).click() except TimeoutException: break # wait for results to load wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.seclection-result .partners-detail'))) print ("Complete") bot.quit()#define Zip for search queryzip = 20855bot = webdriver.Safari()wait = WebDriverWait(bot, 10)#fetch agents pagebot.get('https://www.erieinsurance.com/find-an-insurance-agent')search_agent(zip)load_all_agents()使用上述方法,控制臺會吐出以下錯誤:[錯誤] 拒絕加載https://9203275.fls.doubleclick.net/activityi;src=9203275;type=agent0;cat=agent0;ord=7817740349177;gtm=2wg783;auiddc=373080108.1594822533;~oref=https%3A %2F%2Fwww.erieinsurance.com%2Ffind-an-insurance-agent-results%3Fzipcode%3D20855 ? 因為它沒有出現在內容安全策略的 frame-src 指令中。[錯誤] 拒絕連接到https://api.levelaccess.net/analytics/3.0/results因為它沒有出現在 Content Security Policy 的 connect-src 指令中。
1 回答

青春有我
TA貢獻1784條經驗 獲得超8個贊
創建一個答案來張貼幾張圖片。
當我在 chrome 中運行附加的腳本時,它工作正常。
當@furas 在 firefox 中做同樣的事情時,他得到了同樣的結果
我連續運行同一個腳本 10 次,沒有被拒絕。
我根據錯誤注意到的是 iframe 似乎對瀏覽器敏感:
在 Chrome 中,此標頭包含鉻腳本:
在 Firefox 中它不包含腳本:
看看你在 safari 中手動得到了什么。
一個簡單的答案可能是不使用 safari - 使用 chrome 或 FF。那是一個選擇嗎?(如果它必須是 safari 就說,我會再看一遍。)
最后 - 一些快速的附加說明。
該站點使用的是角度,因此如果您在同步方面遇到困難,則可能需要考慮量角器。(量角器有助于一些腳本同步功能)
還值得一提的是 - 不要覺得您必須登陸主頁然后以用戶身份導航。將您的 URL 更新到搜索結果頁面并輸入郵政編碼,這樣可以節省一些時間:
https://www.erieinsurance.com/find-an-insurance-agent-results?zipcode=20855
[編輯/更新] 這是同一回事嗎?https://github.com/SeleniumHQ/selenium/issues/458 2016 年圍繞“內容安全策略”關閉的錯誤 - 記錄為蘋果的東西。
添加回答
舉報
0/150
提交
取消