我在爬取淘宝的时候,使用selenium打开了一个网页,提取到里面所有的url,模拟点击操作里面的url,当点进去第一个链接以后我把信息提取完就关闭当前这个网页,在第二次循环遍历的时候就报错了。求大神指点这个是为什么?能不能帮我实现一下这个功能,点击完一个url提取信息以后就关闭,继续点击下一个url。直到退出循环
from selenium import webdriver
from time import sleep
import json
from random import randint
class Taobao():
def __init__(self):
self.options = webdriver.ChromeOptions()
self.options.add_experimental_option('excludeSwitches', ['enable-automation'])
self.driver = webdriver.Chrome(options=self.options)
def get_cookies(self, url):
#获取登录cookie
self.driver.get(url)
sleep(20)
with open('tb_cookie.txt', 'w') as f:
f.write(json.dumps(self.driver.get_cookies()))
# self.driver.quit()
def have_cookies(self, url):
#提取需要登录的cookie
self.driver.get(url)
with open('tb_cookie.txt', 'r')as f:
cookie_list = json.loads(f.read())
for cookie in cookie_list:
self.driver.add_cookie({
'domain': '.taobao.com',
'name': cookie['name'],
'value': cookie['value'],
'path': '/',
'expires': None
})
sleep(randint(7,15))
# print(self.driver.page_source)
def have_html(self,url):
self.driver.get(url)
m = self.driver.find_elements_by_xpath('//div[@id="J_u_root"]/div/div/ul/li/a')
for i in m:
sleep(randint(7, 15))
i.click()
#获取当前网页后信息后关闭网页
windows = self.driver.window_handles
self.driver.switch_to.window(windows[1])
sleep(randint(7, 15))
# self.driver.switch_to.window(windows[0])
self.driver.close()
if __name__ == '__main__':
url = 'https://login.taobao.com/member/login.jhtml?'
urls = 'https://ai.taobao.com/search/index.htm?spm=a231o.13503973.search.1&key=手机'
t = Taobao()
# t.get_cookies(url)
t.have_cookies(url)
t.have_html(urls)
以下是报错信息:
selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed
from unknown error: web view not found
(Session info: chrome=72.0.3626.121)
(Driver info: chromedriver=2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1),platform=Windows NT 10.0.19042 x86_64)