我無法取消“查看詳細資訊”按鈕鏈接作為頁面“https://www.bmstores.co.uk/stores?location=KA8 9BF”的串列。多種方式。就我使用的 selenium 而言,使用 x 路徑和 css 選擇器類名查找元素方法,但沒有任何效果。雖然使用 selenium 會出現網站的彈出問題,但它使用彈出視窗阻止程式解決了。
在各個站點搜索,得到相同的beautifulsoup python代碼卻無法完成任務。我的代碼在這里---當我運行時,我收到了 2 個重復錯誤
1.ElementNotInteractableException:元素不可互動 2.NoSuchElementException:訊息:沒有這樣的元素:無法定位元素
我的代碼在這里--
from bs4 import BeautifulSoup
import requests
import pandas as pd
from selenium import webdriver as wd
import time
from selenium.common.exceptions import WebDriverException
local_path_of_chrome_driver = "E:\\chromedriver.exe"
driver = wd.Chrome(executable_path=local_path_of_chrome_driver)
driver.maximize_window()
data_links=[]
xpaths =
["/html/body/div[9]/div/div/div/div/ul/li[1]/div/div[2]/a[1]","/html/body/div[9]/div/div/div/div/ul/li[2]/div/div[2]/a[1]","/html/body/div[9]/div/div/div/div/ul/li[4]/div/div[2]/a[1]","/html/body/div[9]/div/div/div/div/ul/li[5]/div/div[2]/a[1]"]
for j in xpaths:
try:
driver.find_element_by_xpath(j).click()
time.sleep(3)
driver.switch_to_window(driver.window_handles[-1])
data_links.append(driver.current_url)
time.sleep(3)
driver.back()
except:
pass
driver.close()
有人可以幫我嗎?
uj5u.com熱心網友回復:
您可以使用請求模塊獲取所有names及其相關資訊view details button link。共有24家店鋪。
import requests
from urllib.parse import urljoin
base = 'https://www.bmstores.co.uk'
link = 'https://mv7e2a3yql-dsn.algolia.net/1/indexes/*/queries'
params = {
'x-algolia-agent': 'Algolia for JavaScript (3.35.0); Browser; instantsearch.js (3.6.0); JS Helper (2.28.0)',
'x-algolia-application-id': 'MV7E2A3YQL',
'x-algolia-api-key': 'Mzg2ZjM2ZmVmNzhiMmVhZjhhNjQ5ZDAzNGQ5NjE2MTQ1MDQ2ZDAwODBlMjY2YjFkNWFkOTUyOTZkNTRhY2M4MmZpbHRlcnM9JTI4c3RhdHVzJTNBYXBwcm92ZWQlMjkrQU5EK3B1Ymxpc2hkYXRlKyUzQysxNjM1NTAzMzI5K0FORCslMjhleHBpcnlkYXRlKyUzRSsxNjM1NTAzMzI5K09SK2V4cGlyeWRhdGUrJTNEKy0xJTI5',
}
with requests.Session() as s:
s.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'
s.headers['Referer'] = 'https://www.bmstores.co.uk/stores?location=KA8 9BF'
page = 0
while page<=3:
payload = {"requests":[{"indexName":"prod_bmstores_stores","params":f"query=&hitsPerPage=10&page={page}&attributesToRetrieve=*&highlightPreTag=__ais-highlight__&highlightPostTag=__/ais-highlight__&getRankingInfo=true&aroundLatLng=55.47888,-4.59464&aroundRadius=50000&clickAnalytics=false&facets=["ranges"]&tagFilters="}]}
res = s.post(link,params=params,json=payload)
for item in res.json()['results']:
for container in item['hits']:
store_name = container['storename']
detail_link = urljoin(base,container['url'])
print(store_name,detail_link)
page =1
uj5u.com熱心網友回復:
要從頁面https://www.bmstores.co.uk/stores?location=KA8 9BF抓取查看詳細資訊按鈕鏈接作為串列,您必須誘導WebDriverWait,您可以使用以下定位器策略:
代碼塊:
view_details = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.LINK_TEXT, "View Details"))) for i in view_details: print(i.get_attribute("href"))控制臺輸出:
https://www.bmstores.co.uk/stores/ayr-heathfield-retail-park-90 https://www.bmstores.co.uk/stores/prestwick-113 https://www.bmstores.co.uk/stores/irvine-307 https://www.bmstores.co.uk/stores/kilmarnock-310 https://www.bmstores.co.uk/stores/stevenston-319 https://www.bmstores.co.uk/stores/darnley-414 https://www.bmstores.co.uk/stores/east-kilbride-304 https://www.bmstores.co.uk/stores/paisley-linwood-423 https://www.bmstores.co.uk/stores/linwood-hart-street-33 https://www.bmstores.co.uk/stores/paisley-renfrew-road-428
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/344505.html
