用selenium做爬虫的话,要怎么print具有相同CLASS_NAME的多条内容?
时间: 2024-03-31 19:32:02 浏览: 20
如果要打印具有相同class name的多条内容,可以使用`find_elements_by_class_name()`方法,它会返回一个包含所有具有相同class name的元素列表。然后可以使用循环遍历这个列表,打印出每个元素的文本或属性。
以下是一个示例代码:
```python
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://example.com')
elements = driver.find_elements_by_class_name('example-class')
for element in elements:
print(element.text)
```
在这个示例中,`find_elements_by_class_name()`方法用于查找所有具有`example-class`类的元素,并将它们存储在`elements`变量中。然后,使用循环遍历`elements`列表,并使用`element.text`打印每个元素的文本内容。
相关问题
selenium爬虫微博
以下是使用Selenium爬取微博的步骤:
1.安装Selenium和ChromeDriver
```shell
pip install selenium
```
ChromeDriver下载地址:http://chromedriver.chromium.org/downloads
2.导入Selenium库和时间库
```python
from selenium import webdriver
import time
```
3.设置ChromeDriver路径和浏览器选项
```python
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless') # 无头模式,不打开浏览器界面
chrome_options.add_argument('--disable-gpu') # 禁用GPU加速
chrome_options.add_argument('--no-sandbox') # 沙盒模式
chrome_options.add_argument('--disable-dev-shm-usage') # 禁用/dev/shm使用
driver = webdriver.Chrome(executable_path='/path/to/chromedriver', chrome_options=chrome_options)
```
4.打开微博登录页面并登录
```python
driver.get('https://weibo.com/login.php')
time.sleep(10) # 等待页面加载完成
driver.find_element_by_name('username').send_keys('your_username') # 输入用户名
driver.find_element_by_name('password').send_keys('your_password') # 输入密码
driver.find_element_by_class_name('W_btn_a').click() # 点击登录按钮
time.sleep(10) # 等待页面加载完成
```
5.搜索关键词并获取微博内容和评论
```python
driver.get('https://s.weibo.com/weibo?q=your_keyword') # 搜索关键词
time.sleep(10) # 等待页面加载完成
weibo_list = driver.find_elements_by_xpath('//div[@class="content"]/p[@class="txt"]') # 获取微博内容
comment_list = driver.find_elements_by_xpath('//div[@class="content"]/div[@class="card-act"]/ul/li[2]/a') # 获取评论数
for i in range(len(weibo_list)):
print('微博内容:', weibo_list[i].text)
print('评论数:', comment_list[i].text)
```
selenium爬虫实例
这里是一些简单的Selenium爬虫实例:
1.爬取天气数据
```python
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.accuweather.com/')
search_box = driver.find_element_by_name('query')
search_box.send_keys('北京')
search_button = driver.find_element_by_xpath('//*[@id="search-form"]/div/div/div[1]/button/span[1]')
search_button.click()
temp = driver.find_element_by_xpath('//*[@id="detail-day-night"]/div[1]/div/span[1]')
print(temp.text)
driver.quit()
```
2.模拟登录
```python
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://github.com/login')
username_box = driver.find_element_by_id('login_field')
username_box.send_keys('your_username')
password_box = driver.find_element_by_id('password')
password_box.send_keys('your_password')
login_button = driver.find_element_by_xpath('//*[@id="login"]/div[4]/form/div/input[12]')
login_button.click()
# do something after login
driver.quit()
```
3.爬取网页数据
```python
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
search_box = driver.find_element_by_name('wd')
search_box.send_keys('Python')
search_button = driver.find_element_by_xpath('//*[@id="su"]')
search_button.click()
search_results = driver.find_elements_by_xpath('//h3[@class="t"]')
for result in search_results:
title = result.text
link = result.find_element_by_xpath('./a').get_attribute('href')
print(title, link)
driver.quit()
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)