关于selenium模拟浏览器的一些坑(quit,close)

在写爬虫时会经常因为加载js等一些问题,不得不使用selenium模拟浏览器登陆,有的时候会打开多个浏览器,就会导致电脑变卡,内存不够,直接导致程序终止。

这里我以前的文章中说过可以用特定端口打开浏览器,有兴趣的可以去看看,这次主要是说quit和close

如果打开了,能及时关闭,也不会影响到什么,因此,这就用到了这两个方法。首先看一下这两个函数的描述。

    def quit(self):
        """
        Closes the browser and shuts down the ChromeDriver executable
        that is started when starting the ChromeDriver
        """
        try:
            RemoteWebDriver.quit(self)
        except Exception:
            # We don't care about the message because something probably has gone wrong
            pass
        finally:
            self.service.stop()

这里的意思就是说quit函数会把你打开浏览器的所有执行的操作全部停掉并且退出浏览器

    def close(self):
        """
        Closes the current window.

        :Usage:
            driver.close()
        """
        self.execute(Command.CLOSE)

这里的意思是说close只会把当前的端口关闭

总结

两个的区别就是close是关闭当前窗口,如果只有一个窗口就会关闭浏览器,但并没有清除一些配置。而quit则是退出浏览器,并且清除了所有的配置。
因此,根据需求选择quit和close

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,感谢您对ChitGPT的关注和使用。 如您所述,需要使用Selenium模拟浏览器访问京东首页,然后爬取商品信息以及评价信息,并将数据保存到MySQL数据库中。这个任务需要使用Python编写爬虫脚本,并结合Selenium和BeautifulSoup等库进行数据的抓取和解析,在获取到数据后再将其存储到MySQL数据库中。 首先,需要安装Python和相关库,包括Selenium、BeautifulSoup、pymysql等。然后,需要启动Selenium模拟浏览器,并模拟用户在京东首页搜索关键词“口罩”,然后在搜索结果页面中解析每个商品的详细参数,包括标题、价格、店名等,并获取每个商品的好评度和好评详细。最后,将获取到的数据保存到MySQL数据库中。 下面是一个例子,供您参考: ```python from selenium import webdriver from bs4 import BeautifulSoup import pymysql import time # 启动Selenium模拟浏览器 driver = webdriver.Chrome() driver.get('https://item.jd.com') # 在搜索框中输入关键词 search_input = driver.find_element_by_id('key') search_input.send_keys('口罩') # 点击搜索按钮 search_btn = driver.find_element_by_class_name('button') search_btn.click() # 等待页面加载完成 time.sleep(5) # 解析搜索结果页面 html = driver.page_source soup = BeautifulSoup(html, 'html.parser') items = soup.find('div', {'id': 'J_goodsList'}).find_all('li') # 将商品信息和评价信息保存到MySQL数据库中 conn = pymysql.connect(host='localhost', user='root', password='123456', database='mydb', charset='utf8') cursor = conn.cursor() for item in items[:100]: # 解析商品标题、价格、店名等信息 title = item.find('div', {'class': 'p-name'}).text price = item.find('strong', {'class': 'J_price'}).i.text shop = item.find('div', {'class': 'p-shop'}).span.a.text # 进入商品详情页获取好评信息 detail_link = item.find('div', {'class': 'p-name'}).a['href'] driver.get(detail_link) # 等待页面加载完成 time.sleep(3) # 解析商品好评信息 detail_html = driver.page_source detail_soup = BeautifulSoup(detail_html, 'html.parser') comment = detail_soup.find('div', {'class': 'comment-item'}).find('div', {'class': 'comment-content'}).text rate = detail_soup.find('div', {'class': 'comment-item'}).find('div', {'class': 'comment-star'}).em['class'][1].split('-')[-1] # 将信息保存到MySQL数据库中 sql = "INSERT INTO products(title, price, shop, comment, rate) VALUES (%s, %s, %s, %s, %s)" cursor.execute(sql, (title, price, shop, comment, rate)) conn.commit() # 关闭数据库连接和浏览器 cursor.close() conn.close() driver.quit() ``` 这个例子中,我们首先启动了Selenium模拟浏览器,并在京东首页搜索关键词“口罩”,然后解析搜索结果页面的商品信息,包括标题、价格、店名等,并获取每个商品的详情页链接。接着,我们循环遍历商品列表,并进入每个商品的详情页,解析其中的好评信息,包括好评度和好评详细,并将获取到的数据保存到MySQL数据库中。最后,我们关闭数据库连接和浏览器。 希望这个例子能够帮助您完成您的任务。如果您有任何问题或建议,请随时联系我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值