案例一:获取网页内容
基础知识
爬虫技术的基础是获取网页内容。我们可以使用Python的requests库来发送HTTP请求,获取网页内容。
实战步骤
- 导入
requests库。 - 使用
requests.get()方法发送请求。 - 获取响应对象。
- 使用响应对象的
.text属性获取网页内容。
代码示例
import requests
url = 'https://www.example.com'
response = requests.get(url)
html_content = response.text
案例二:解析网页内容
基础知识
获取网页内容后,我们需要解析网页内容,提取所需信息。Python的BeautifulSoup库可以方便地解析HTML和XML文档。
实战步骤
- 导入
BeautifulSoup库。 - 创建
BeautifulSoup对象。 - 使用选择器提取所需信息。
代码示例
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_content, 'html.parser')
title = soup.find('title').text
print(title)
案例三:模拟登录
基础知识
有些网站需要登录才能访问特定页面。我们可以使用requests.Session()来模拟登录。
实战步骤
- 创建
Session对象。 - 发送登录请求,携带登录信息。
- 使用登录后的
Session对象访问受保护的页面。
代码示例
session = requests.Session()
login_url = 'https://www.example.com/login'
login_data = {
'username': 'your_username',
'password': 'your_password'
}
session.post(login_url, data=login_data)
protected_page = session.get('https://www.example.com/protected')
案例四:处理JavaScript渲染的页面
基础知识
有些网站使用JavaScript动态渲染页面内容。我们可以使用Selenium库来模拟浏览器行为。
实战步骤
- 安装
Selenium和对应的WebDriver。 - 创建
WebDriver实例。 - 使用
WebDriver访问目标页面。 - 使用
WebDriver的API提取所需信息。
代码示例
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.example.com')
title = driver.find_element_by_tag_name('title').text
print(title)
driver.quit()
案例五:下载图片和视频
基础知识
爬虫技术可以用于下载图片和视频。我们可以使用requests库的流式下载功能。
实战步骤
- 使用
requests.get()方法发送请求。 - 使用响应对象的
.stream()方法获取流式数据。 - 使用
open()函数打开文件,写入流式数据。
代码示例
url = 'https://www.example.com/image.jpg'
response = requests.get(url, stream=True)
with open('image.jpg', 'wb') as f:
for chunk in response.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
案例六:爬取商品信息
基础知识
爬虫技术可以用于爬取商品信息,如价格、库存等。
实战步骤
- 使用
requests库获取商品页面内容。 - 使用
BeautifulSoup库解析商品页面,提取商品信息。
代码示例
soup = BeautifulSoup(html_content, 'html.parser')
title = soup.find('h1', class_='product-title').text
price = soup.find('span', class_='product-price').text
print(title, price)
案例七:爬取新闻列表
基础知识
爬虫技术可以用于爬取新闻列表,如标题、链接、发布时间等。
实战步骤
- 使用
requests库获取新闻列表页面内容。 - 使用
BeautifulSoup库解析新闻列表页面,提取新闻信息。
代码示例
soup = BeautifulSoup(html_content, 'html.parser')
news_list = soup.find_all('div', class_='news-item')
for news in news_list:
title = news.find('h3').text
link = news.find('a')['href']
print(title, link)
案例八:爬取股票信息
基础知识
爬虫技术可以用于爬取股票信息,如股票代码、名称、最新价格等。
实战步骤
- 使用
requests库获取股票信息页面内容。 - 使用
BeautifulSoup库解析股票信息页面,提取股票信息。
代码示例
soup = BeautifulSoup(html_content, 'html.parser')
stock_list = soup.find_all('tr', class_='stock-item')
for stock in stock_list:
code = stock.find('td', class_='stock-code').text
name = stock.find('td', class_='stock-name').text
price = stock.find('td', class_='stock-price').text
print(code, name, price)
案例九:爬取天气预报
基础知识
爬虫技术可以用于爬取天气预报信息,如温度、湿度、风力等。
实战步骤
- 使用
requests库获取天气预报页面内容。 - 使用
BeautifulSoup库解析天气预报页面,提取天气预报信息。
代码示例
soup = BeautifulSoup(html_content, 'html.parser')
weather_list = soup.find_all('div', class_='weather-item')
for weather in weather_list:
city = weather.find('span', class_='city').text
temperature = weather.find('span', class_='temperature').text
humidity = weather.find('span', class_='humidity').text
wind = weather.find('span', class_='wind').text
print(city, temperature, humidity, wind)
案例十:爬取微博信息
基础知识
爬虫技术可以用于爬取微博信息,如用户名、昵称、微博内容等。
实战步骤
- 使用
requests库获取微博页面内容。 - 使用
BeautifulSoup库解析微博页面,提取微博信息。
代码示例
soup = BeautifulSoup(html_content, 'html.parser')
weibo_list = soup.find_all('div', class_='weibo-item')
for weibo in weibo_list:
user_name = weibo.find('a', class_='user-name').text
nickname = weibo.find('a', class_='nickname').text
content = weibo.find('p', class_='content').text
print(user_name, nickname, content)
