python selenium爬虫案例
时间: 2023-04-28 18:05:20 浏览: 117
以爬取百度首页为例:
1. 安装Selenium: pip install selenium
2. 下载对应浏览器的驱动(如Chrome浏览器的驱动为chromedriver)
3. 编写代码:
```python
from selenium import webdriver
# 配置驱动
driver = webdriver.Chrome(executable_path='path/to/chromedriver')
# 打开网页
driver.get("https://www.baidu.com")
# 获取页面源代码
html = driver.page_source
# 关闭浏览器
driver.close()
```
这样就可以使用 Selenium 爬取百度首页的源代码了.
不过要注意的是爬取动态加载页面需要使用更复杂的方法,如等待元素加载等。
相关问题
python的selenium爬虫案例
Python的Selenium库是一个强大的工具,可以用于自动化网页操作和数据采集。下面是一个 Python的Selenium爬虫案例:
案例1:京东页面采集
首先,您需要安装Selenium库[2.1]。然后,您可以使用以下步骤编写Python脚本来实现京东页面的数据采集:
1. 导入必要的模块:
from selenium import webdriver
2. 创建一个浏览器驱动实例,并打开京东的首页:
driver = webdriver.Chrome()
driver.get("https://www.jd.com/")
3. 定位并点击搜索框,并输入关键词:
search_box = driver.find_element_by_id("key")
search_box.send_keys("手机")
search_box.submit()
4. 定位商品列表,并获取列表中的商品信息:
product_list = driver.find_elements_by_class_name("gl-item")
for product in product_list:
# 获取商品名称
name = product.find_element_by_class_name("p-name").text
# 获取商品价格
price = product.find_element_by_class_name("p-price").text
# 打印商品信息
print("商品名称:", name)
print("商品价格:", price)
5. 关闭浏览器:
driver.quit()
这个案例演示了如何使用Selenium库来打开京东首页,搜索关键词并获取商品列表的名称和价格[2.2]。通过定位元素,您可以轻松地获取您所需的数据。
请注意,由于Selenium库是一个自动化工具,它的效率较低,因此适用于小规模的数据采集任务。
希望这个案例能为您提供一个基本的了解,您可以根据自己的需求进行相应的修改和扩展。
python selenium多线程爬虫案例
下面是一个简单的Python Selenium多线程爬虫案例:
```python
import threading
from selenium import webdriver
class MyThread(threading.Thread):
def __init__(self, url):
threading.Thread.__init__(self)
self.url = url
def run(self):
driver = webdriver.Chrome()
driver.get(self.url)
# 在这里处理你的爬虫逻辑
driver.quit()
urls = ["http://www.example.com/page1", "http://www.example.com/page2", "http://www.example.com/page3"]
threads = []
for url in urls:
threads.append(MyThread(url))
for thread in threads:
thread.start()
for thread in threads:
thread.join()
```
在这个例子中,我们创建了一个自定义的线程类MyThread。这个类继承了Python threading模块中的Thread类,并重写了它的run()方法。在线程启动时,它将使用Selenium webdriver启动一个Chrome浏览器,并访问指定的URL。你可以在run()方法中添加你的爬虫逻辑。
最后,我们创建了一个列表来存储所有的线程,并依次启动它们。我们使用join()方法等待所有线程的执行完成。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)