学习笔记:网络爬虫
网络爬虫
安装Python:没有Python,一切白搭!
首先,你需要确保你的电脑上已经安装了Python(推荐3.7+版本)。没有安装?赶紧去Python官网下载安装包,一键安装,so easy!
安装必要库:一键安装,解放双手!
1
pip install requests beautifulsoup4
requests:负责发送HTTP请求,与网站建立连接。
beautifulsoup4:负责解析HTML数据,提取网页内容。
撸起袖子就是干:第一个爬虫脚本诞生!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28import requests
from bs4 import BeautifulSoup
def simple_crawler(url):
try:
# 1. 发送请求
response = requests.get(url)
response.raise_for_status() # 检查请求是否成功
# 2. 解析网页内容
soup = BeautifulSoup(response.text, 'html.parser')
# 3. 提取标题和段落内容
title = soup.find('title').text # 获取网页标题
paragraphs = soup.find_all('p') # 获取所有段落内容
print(f"网页标题: {title}
")
print("网页内容:")
for p in paragraphs:
print(p.text)
except requests.exceptions.RequestException as e:
print(f"请求失败: {e}")
# 示例网址
url = "https://example.com" # 替换为你想要爬取的网页地址
simple_crawler(url)
- 感谢你赐予我前进的力量
赞赏名单
因为您的支持让我意识到写文章的价值😉
本文是原创文章,采用CC BY-NC-SA 4.0协议,完整转载请注明来自云轻無限
评论








