使用 selenium webdriver 抓取多个页面时出现问题 - python
Problem with scraping multiple pages with selenium webdriver - python
我正在尝试抓取网页和该网页中的链接。网页是:https://webgate.ec.europa.eu/rasff-window/screen/list。如果您注意到有大约 6000 多条通知,并且这些通知具有与之关联的单独链接。我想将所有链接存储在列表中。我正在使用此代码执行此操作:
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
import time
from webdriver_manager.chrome import ChromeDriverManager
d = webdriver.Chrome(ChromeDriverManager().install())
#trying this scraping for multiple pages
links = []
i = 1
elems = d.find_elements_by_xpath("//a[@href]")
for elem in elems:
link_list = elem.get_attribute("href")
links.append(link_list)
while True:
print("This is the now the {} page".format(i))
i +=1
time.sleep(1)
try:
time.sleep(0.5)
WebDriverWait(d, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='Next page']"))).click()
print("we have clicked it once")
time.sleep(0.9)
elems2 = d.find_elements_by_xpath("//a[@href]")
for elem2 in elems2:
link_list = elem2.get_attribute("href")
links.append(link_list)
print("The button is clickable")
time.sleep(1)
except:
print("The button is now not clickable, we have collected all the links")
break
我的想法是使用 selenium 首先从该页面找到所有 href 链接,然后单击下一页按钮并执行相同操作,我的 While 循环就是这样做的。但是当我 运行 这段代码时,它并没有完成整个循环。例如:如果有大约 6400 个通知,我希望它 运行 到第 64 页,但它停在中间,表明下一个按钮不可点击(条件除外),尽管实际上按钮是可点击的。这发生在随机页面上,我也尝试更改 time.sleep。我做错了什么吗?
我检查了来自异常的消息
except Exception as ex:
print(ex)
它表明问题不是 button
而是 href
似乎有时它在 JavaScript
更新页面上的所有元素之前获取对 <a>
的引用 - 接下来当它尝试从 <a>
获取 href
然后出错显示此 <a>
在页面上不存在,因为同时 JavaScript
将其删除并放入新的 <a>
.
并且检查按钮是否可点击可能是无用的,因为它一直存在。
在得到 <a>
之前,你应该多睡一会儿。或者你会找到更好的方法来检测你是否获得了新的引用或与以前相同。
我正在尝试抓取网页和该网页中的链接。网页是:https://webgate.ec.europa.eu/rasff-window/screen/list。如果您注意到有大约 6000 多条通知,并且这些通知具有与之关联的单独链接。我想将所有链接存储在列表中。我正在使用此代码执行此操作:
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
import time
from webdriver_manager.chrome import ChromeDriverManager
d = webdriver.Chrome(ChromeDriverManager().install())
#trying this scraping for multiple pages
links = []
i = 1
elems = d.find_elements_by_xpath("//a[@href]")
for elem in elems:
link_list = elem.get_attribute("href")
links.append(link_list)
while True:
print("This is the now the {} page".format(i))
i +=1
time.sleep(1)
try:
time.sleep(0.5)
WebDriverWait(d, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='Next page']"))).click()
print("we have clicked it once")
time.sleep(0.9)
elems2 = d.find_elements_by_xpath("//a[@href]")
for elem2 in elems2:
link_list = elem2.get_attribute("href")
links.append(link_list)
print("The button is clickable")
time.sleep(1)
except:
print("The button is now not clickable, we have collected all the links")
break
我的想法是使用 selenium 首先从该页面找到所有 href 链接,然后单击下一页按钮并执行相同操作,我的 While 循环就是这样做的。但是当我 运行 这段代码时,它并没有完成整个循环。例如:如果有大约 6400 个通知,我希望它 运行 到第 64 页,但它停在中间,表明下一个按钮不可点击(条件除外),尽管实际上按钮是可点击的。这发生在随机页面上,我也尝试更改 time.sleep。我做错了什么吗?
我检查了来自异常的消息
except Exception as ex:
print(ex)
它表明问题不是 button
而是 href
似乎有时它在 JavaScript
更新页面上的所有元素之前获取对 <a>
的引用 - 接下来当它尝试从 <a>
获取 href
然后出错显示此 <a>
在页面上不存在,因为同时 JavaScript
将其删除并放入新的 <a>
.
并且检查按钮是否可点击可能是无用的,因为它一直存在。
在得到 <a>
之前,你应该多睡一会儿。或者你会找到更好的方法来检测你是否获得了新的引用或与以前相同。