使用 Selenium 和 Python 进行重复搜索。给我那些他们没有结果的
Repetitive search using Selenium and Python. And give me ones that they have no results
我想在搜索字段中输入 Samsung *** Plus
,然后按网站中的搜索按钮。 (重复)
此站点没有任何验证码,不会转到其他页面以获取搜索结果。
每次在此循环中,我都希望提交一封信或一个数字,而不是这三颗星。
这意味着它搜索了 46,656 次。 ((25+10).(25+10).(25+10))
并打印导致 "Not found!" paragraph
.
的结果
PS。有更简单的方法吗?
通常最好提供更多信息(包括URL、您尝试过的具体代码等)。
首先设置您的网络驱动程序和页面
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('www.yoururl.com')
现在我们将找到所需的元素。您可以使用任何您喜欢的方法,但我会使用名称。
search_box = driver.find_element_by_name('searchBoxName')
search_button = driver.find_element_by_name('searchButtonName')
现在我们将在返回结果的同时遍历您搜索的不同数字。这是循环的开始
for i in range(46656): #based on the number you gave
search_box.send_keys('Samsung {} Plus'.format(i))
search_button.click()
返回结果有点棘手,考虑到您并没有真正提供太多信息。有两种主要方法可以解决这个问题。
选项 1:找到负责给出搜索结果的元素并获取 .text
属性。这是该示例的 for 循环。
for i in range(46656): #based on the number you gave
search_box.send_keys('Samsung {} Plus'.format(i))
search_button.click()
result = driver.find_element_by_name('result')
if result.text == 'Not Found!':
print("Samsung {} Plus: Not Found".format(i))
选项2:如果某个元素仅在没有结果时出现,您可以尝试定位该元素并使用它的存在来标记结果的缺失。如果仅当有结果时才存在特定元素,您也可以向后执行此操作。
for i in range(46656): #based on the number you gave
search_box.send_keys('Samsung {} Plus'.format(i))
search_button.click()
try: #look for the element that is present when there are no results
no_results = driver.find_element_by_name('noResults')
print("Samsung {} Plus: Not found!".format(i))
except NoSuchElementException: #if element is not found, there are results
pass
如果选择选项 2,则需要在代码中添加以下行:
from selenium.common.exceptions import NoSuchElementException
我想在搜索字段中输入 Samsung *** Plus
,然后按网站中的搜索按钮。 (重复)
此站点没有任何验证码,不会转到其他页面以获取搜索结果。
每次在此循环中,我都希望提交一封信或一个数字,而不是这三颗星。
这意味着它搜索了 46,656 次。 ((25+10).(25+10).(25+10))
并打印导致 "Not found!" paragraph
.
PS。有更简单的方法吗?
通常最好提供更多信息(包括URL、您尝试过的具体代码等)。
首先设置您的网络驱动程序和页面
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('www.yoururl.com')
现在我们将找到所需的元素。您可以使用任何您喜欢的方法,但我会使用名称。
search_box = driver.find_element_by_name('searchBoxName')
search_button = driver.find_element_by_name('searchButtonName')
现在我们将在返回结果的同时遍历您搜索的不同数字。这是循环的开始
for i in range(46656): #based on the number you gave
search_box.send_keys('Samsung {} Plus'.format(i))
search_button.click()
返回结果有点棘手,考虑到您并没有真正提供太多信息。有两种主要方法可以解决这个问题。
选项 1:找到负责给出搜索结果的元素并获取 .text
属性。这是该示例的 for 循环。
for i in range(46656): #based on the number you gave
search_box.send_keys('Samsung {} Plus'.format(i))
search_button.click()
result = driver.find_element_by_name('result')
if result.text == 'Not Found!':
print("Samsung {} Plus: Not Found".format(i))
选项2:如果某个元素仅在没有结果时出现,您可以尝试定位该元素并使用它的存在来标记结果的缺失。如果仅当有结果时才存在特定元素,您也可以向后执行此操作。
for i in range(46656): #based on the number you gave
search_box.send_keys('Samsung {} Plus'.format(i))
search_button.click()
try: #look for the element that is present when there are no results
no_results = driver.find_element_by_name('noResults')
print("Samsung {} Plus: Not found!".format(i))
except NoSuchElementException: #if element is not found, there are results
pass
如果选择选项 2,则需要在代码中添加以下行:
from selenium.common.exceptions import NoSuchElementException