【问题标题】:How to grab an output result from website using selenium如何使用硒从网站获取输出结果
【发布时间】:2022-11-23 19:59:27
【问题描述】:
所以我想尝试这段代码。如果网站存在,它会输出可用的域名。我用过这个网站www.eurodns.com/whois-search/app-domain-name
如果该网站不存在、当前停放或注册,它会这样说。
我正在考虑的代码涉及 selenium 和 chrome 驱动程序输入文本并进行搜索。
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
cli = ['https://youtube.com', 'https://google.com', 'https://minecraft.net', 'https://something.odoo.com']
Exists = []
for i in cli:
driver.get("https://www.eurodns.com/whois-search/app-domain-name")
Name = driver.find_element(By.CSS_SELECTOR, "input[name='whoisDomainName']")
Name.send_keys(cli)
driver.find_element(By.XPATH,/html/body/div/div[3]/div/div[2]/form/div/div/div/button).click()
有没有一种方法,例如if website available, exist.append(cli),elif web not valid, print('Not valid'),以便它可以过滤掉存在的网站和不存在的网站。我正在考虑使用 beautifulsoup 来获取输出,但我不确定如何正确使用它。
谢谢!
【问题讨论】:
标签:
python
selenium
jupyter-notebook
【解决方案1】:
无需使用其他库。
而不是那样使用 XPATH,因为它可能会改变页面的结构。始终尝试通过 ID 搜索元素,如果它存在与该特定元素相关联(根据它们的性质在页面上应该是唯一的)或通过类名(如果它看起来是唯一的)或通过名称属性。
关于算法的一些说明:
-
我们可以访问一次首页,然后不时提交url。因此我们节省了执行时间。
-
每当我们提交一个 url 时,我们只需要验证该 url 不存在(或者它存在)
-
Name the variables in a more conversational/descriptive way。
-
注意向站点快速发送过多请求。它可能会阻止你。也许这不是您任务的正确方法?是否没有可用于此类服务的 API?
您的代码变为:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import time
opts = Options()
# make web scraping 'invisible' if GUI is not required
opts.add_argument("--headless")
opts.add_argument('--no-sandbox')
user_agent = "user-agent=[Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36]"
opts.add_argument(user_agent)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=opts)
urls = ['https://youtube.com', 'https://google.com', 'https://minecraft.net', 'https://something.odoo.com']
exists = []
driver.get("https://www.eurodns.com/whois-search/app-domain-name")
for url in urls:
# send url to textarea
textarea = driver.find_element(By.NAME, "whoisDomainName")
textarea.clear() # make sure to clear textarea
textarea.send_keys(url)
# click 'WHOIS LOOKUP' button
driver.find_element(By.ID, "submitBasic").click()
# try to find error message (wait 3 sec)
try:
WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.CLASS_NAME, 'whoisSearchError')))
print(f'URL {url} is not valid')
except TimeoutException:
print(f'URL {url} is valid')
exists.append(url)
time.sleep(30) # wait 30 seconds to avoid '429 too many requests'
print(f"
URLs that exist:
", exists)
输出将是:
URL https://youtube.com is valid
URL https://google.com is valid
URL https://minecraft.net is valid
URL https://something.odoo.com is not valid
URLs that exist:
['https://youtube.com', 'https://google.com', 'https://minecraft.net']