selenium爬虫实例
时间: 2023-05-24 09:06:56 浏览: 195
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()
```
阅读全文