Python - webCrawler - driver.close 语法错误
Python - webCrawler - driver.close incorrect syntax
新手程序员,正在做一个WebCrawler
,想出了
driver.close()
^语法错误如下所示,
不过,我用上面的驱动没有问题,所以我现在很困惑
感谢所有能得到的帮助
提前感谢团队
import sys
from selenium import webdriver as wd
# from bs4 import BeautifulSoup as bs
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# import pymysql as my
import time
# from Tour import TourInfo
# First Url =
# https://stamprally.org/?search_keywords=&search_keywords_operator=and&search_cat1=145&search_cat2=0
# Second Url =
# https://stamprally.org/?search_keywords=&search_keywords_operator=and&search_cat1=66&search_cat2=0
driver.get(main_url)
try:
element = WebDriverWait(driver, 10).until(
# 지정한 요소 한개가 발견되면 웨이트 종료
EC.presence_of_element_located((By.ID, 'header_search_cat1'))
)
except Exception as e:
print('오류 발생', e)
driver.implicitly_wait(10)
for prefectureValue in range(66, 121):
offshorePrefectureValue = 145
try:
driver.get(
f"https://stamprally.org/?search_keywords=&search_keywords_operator=and&search_cat1={prefectureValue}&search_cat2=0)")
print(driver.current_url)
# close off
driver.close()
driver.quit()
sys.exit()
SyntaxError: invalid syntax
PS C:\Users\eong\Desktop\stamprally_crawl> python run.py
File "C:\Users\eong\Desktop\stamprally_crawl\run.py", line 70
driver.close()
^
SyntaxError: invalid syntax
如果你只打开了单曲 window 你在执行 driver.close()
之后没有任何东西可以 driver.quit()
try 也需要有一个 except(或 finally)块。因此,要么删除 try,要么添加一个 except 来捕获错误。
不敢相信我花了这么多时间寻找语法,却错过了该死的 catchException 代码,以为我不需要异常捕获,因为我知道这不会出错
从循环中删除它(缩进问题)。我认为这是主要的错误。
driver.close()
driver.quit()
sys.exit()
它在循环的第一个循环后退出驱动程序。
在这里重述 quit()
和 close()
之间的区别: Difference between webdriver.Dispose(), .Close() and .Quit()
此外,如果您的条件正确,请输入 break
。
此外,您声明了 offshorePrefectureValue
但并未真正使用它。
另一个问题与你的问题无关:
try:
element = WebDriverWait(driver, 10).until(
# 지정한 요소 한개가 발견되면 웨이트 종료
EC.presence_of_element_located((By.ID, 'header_search_cat1'))
)
except Exception as e:
print('오류 발생', e)
这里也不需要try/except
。如果元素不存在,Selenium 将抛出异常。
新手程序员,正在做一个WebCrawler
,想出了
driver.close()
^语法错误如下所示,
不过,我用上面的驱动没有问题,所以我现在很困惑
感谢所有能得到的帮助
提前感谢团队
import sys
from selenium import webdriver as wd
# from bs4 import BeautifulSoup as bs
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# import pymysql as my
import time
# from Tour import TourInfo
# First Url =
# https://stamprally.org/?search_keywords=&search_keywords_operator=and&search_cat1=145&search_cat2=0
# Second Url =
# https://stamprally.org/?search_keywords=&search_keywords_operator=and&search_cat1=66&search_cat2=0
driver.get(main_url)
try:
element = WebDriverWait(driver, 10).until(
# 지정한 요소 한개가 발견되면 웨이트 종료
EC.presence_of_element_located((By.ID, 'header_search_cat1'))
)
except Exception as e:
print('오류 발생', e)
driver.implicitly_wait(10)
for prefectureValue in range(66, 121):
offshorePrefectureValue = 145
try:
driver.get(
f"https://stamprally.org/?search_keywords=&search_keywords_operator=and&search_cat1={prefectureValue}&search_cat2=0)")
print(driver.current_url)
# close off
driver.close()
driver.quit()
sys.exit()
SyntaxError: invalid syntax
PS C:\Users\eong\Desktop\stamprally_crawl> python run.py
File "C:\Users\eong\Desktop\stamprally_crawl\run.py", line 70
driver.close()
^
SyntaxError: invalid syntax
如果你只打开了单曲 window 你在执行 driver.close()
driver.quit()
try 也需要有一个 except(或 finally)块。因此,要么删除 try,要么添加一个 except 来捕获错误。
不敢相信我花了这么多时间寻找语法,却错过了该死的 catchException 代码,以为我不需要异常捕获,因为我知道这不会出错
从循环中删除它(缩进问题)。我认为这是主要的错误。
driver.close()
driver.quit()
sys.exit()
它在循环的第一个循环后退出驱动程序。
在这里重述 quit()
和 close()
之间的区别: Difference between webdriver.Dispose(), .Close() and .Quit()
此外,如果您的条件正确,请输入 break
。
此外,您声明了 offshorePrefectureValue
但并未真正使用它。
另一个问题与你的问题无关:
try:
element = WebDriverWait(driver, 10).until(
# 지정한 요소 한개가 발견되면 웨이트 종료
EC.presence_of_element_located((By.ID, 'header_search_cat1'))
)
except Exception as e:
print('오류 발생', e)
这里也不需要try/except
。如果元素不存在,Selenium 将抛出异常。