我在下面的代码中使用driver.close()在代码完成时关闭firefox浏览器,但是在第一次循环后抛出错误。我正确使用它吗?
错误:
ConnectionRefusedError:[WinError 10061]无法建立连接 因为目标机器主动拒绝它
代码:
driver = webdriver.Firefox()
print('Firefox started')
print('Iterating links')
connection = pymysql.connect(host='.com', user='', password='', db='', cursorclass=pymysql.cursors.DictCursor)
with connection.cursor() as cursor2:
cursor2.execute("Delete from todaysmarkets")
for link in links:
try:
print('Fetching from link: ' + base + link)
driver.get(base+link)
print('Waiting for all the data to get loaded')
element = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CLASS_NAME,"current")))
print('Parsing page')
tree = html.fromstring(driver.page_source)
names = tree.xpath('//*[@id="lpTable1"]/div[h3[text()=" Goal Supremacy and Goal Markets "]]/div/table/tbody/tr[not(@style="display: none;")]/td/span/text()')
print(str(len(names)) + ' markets found')
sells = tree.xpath('//*[@id="lpTable1"]/div[h3[text()=" Goal Supremacy and Goal Markets "]]/div/table/tbody/tr[not(@style="display: none;")]/td[3]/button/text()')
buys = tree.xpath('//*[@id="lpTable1"]/div[h3[text()=" Goal Supremacy and Goal Markets "]]/div/table/tbody/tr[not(@style="display: none;")]/td[4]/button/text()')
if(len(buys) != len(sells) or (len(buys) == 0 or len(sells) == 0)):
print('Error fetching markets either suspended or does not exist')
continue
print('Putting markets into excel file')
with connection.cursor() as cursor:
for i in range(0, len(names)):
cursor.execute(("INSERT INTO todaysmarkets(URL,Name,value) VALUES(%s,%s,%s)"), (base+link,names[i], str((float(sells[i])+float(buys[i]))/2.0)))
# ws.append([names[i], str((float(sells[i])+float(buys[i]))/2.0)])
finally:
print('Saving the file with name markets.xlsx')
driver.close()
答案 0 :(得分:1)
您对driver.close()
(应该是driver.quit()
)的来电过早了。
完成所有工作后,您需要完成for
。
driver = webdriver.Firefox()
print('Firefox started')
print('Iterating links')
connection = pymysql.connect(host='.com', user='', password='', db='', cursorclass=pymysql.cursors.DictCursor)
with connection.cursor() as cursor2:
cursor2.execute("Delete from todaysmarkets")
for link in links:
try:
print('Fetching from link: ' + base + link)
driver.get(base+link)
print('Waiting for all the data to get loaded')
element = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CLASS_NAME,"current")))
print('Parsing page')
tree = html.fromstring(driver.page_source)
names = tree.xpath('//*[@id="lpTable1"]/div[h3[text()=" Goal Supremacy and Goal Markets "]]/div/table/tbody/tr[not(@style="display: none;")]/td/span/text()')
print(str(len(names)) + ' markets found')
sells = tree.xpath('//*[@id="lpTable1"]/div[h3[text()=" Goal Supremacy and Goal Markets "]]/div/table/tbody/tr[not(@style="display: none;")]/td[3]/button/text()')
buys = tree.xpath('//*[@id="lpTable1"]/div[h3[text()=" Goal Supremacy and Goal Markets "]]/div/table/tbody/tr[not(@style="display: none;")]/td[4]/button/text()')
if(len(buys) != len(sells) or (len(buys) == 0 or len(sells) == 0)):
print('Error fetching markets either suspended or does not exist')
continue
print('Putting markets into excel file')
with connection.cursor() as cursor:
for i in range(0, len(names)):
cursor.execute(("INSERT INTO todaysmarkets(URL,Name,value) VALUES(%s,%s,%s)"), (base+link,names[i], str((float(sells[i])+float(buys[i]))/2.0)))
# ws.append([names[i], str((float(sells[i])+float(buys[i]))/2.0)])
finally:
print('Saving the file with name markets.xlsx')
# Close driver at the end of the work
driver.quit()