我想进行一些基本的URL验证,如果URL无效,除非用户输入了有效的URL,否则不应继续进行请求。
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
user_url = input('Please enter a valid url:')
driver = webdriver.Chrome('/home/m/Desktop/chromedriver')
driver.get(user_url)
HEADERS = {'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36', 'accept': '*/*'}
time.sleep(8)
imagecounter = driver.find_elements_by_css_selector('img')
print('Number of HTML image tags:')
print(len(imagecounter))
您能否修改代码并解释发生了什么?我已经尝试过一些库,但是我认为由于我的编码技巧很差,所以没有运气。
您可以使用请求获取HTTP状态代码
import requests
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
user_url = input('Please enter a valid url:')
req = requests.get(user_url)
while req.status_code != requests.codes['ok']:
user_url = input('Please enter a valid url:')
driver = webdriver.Chrome()
driver.get(user_url)
HEADERS = {'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36', 'accept': '*/*'}
time.sleep(8)
imagecounter = driver.find_elements_by_css_selector('img')
print('Number of HTML image tags:')
print(len(imagecounter))