👉博__主👈:米码收割机
👉技__能👈:C++/Python语言
👉公众号👈:测试开发自动化【获取源码+商业合作】
👉荣__誉👈:阿里云博客专家博主、51CTO技术博主
👉专__注👈:专注主流机器人、人工智能等相关领域的开发、测试技术。
爬虫(框架)爬取网站页面
目录
- 爬虫(框架)爬取网站页面
 - 爬虫(框架)爬取网站页面
 
- 1. 导入必要的库
 - 2. 获取网页内容
 - 3. 使用BeautifulSoup解析HTML
 - 4. 数据提取
 - 5. 异常处理
 - 6. 避免被封禁
 - 完整代码示例:
 - 注意事项
 - <center>本期好书推荐《Python网络爬虫入门实战》
 
- <center>购买链接
 
- <center>京东:https://item.jd.com/14049708.html
 
爬虫(框架)爬取网站页面
1. 导入必要的库
import requests
from bs4 import BeautifulSoup
 
requests库用于发送HTTP请求以获取网页内容。BeautifulSoup库用于解析HTML内容并提取我们需要的信息。
2. 获取网页内容
我们首先要使用requests库获取页面的HTML内容。
url = 'https://example.com/articles'
response = requests.get(url)
html_content = response.content
 
3. 使用BeautifulSoup解析HTML
将获取到的HTML内容传递给BeautifulSoup,这样我们就可以用它来解析页面了。
soup = BeautifulSoup(html_content, 'html.parser')
 
4. 数据提取
这完全取决于你想从页面中提取哪些信息。假设我们要提取所有文章标题和链接:
articles = soup.find_all('div', class_='article')  # 假设每篇文章都包含在一个class为'article'的div标签内
for article in articles:
    title = article.find('h2').text
    link = article.find('a')['href']
    print(title, link)
 
5. 异常处理
在爬取网站时可能会遇到各种问题,如网络问题、页面不存在等。我们需要添加一些异常处理来确保爬虫的稳定性。
try:
    response = requests.get(url, timeout=10)
    response.raise_for_status()  # 如果响应状态不是200,则引发异常
except requests.RequestException as e:
    print(f"Error fetching the url: {url}. Reason: {e}")
 
6. 避免被封禁
当连续并频繁请求某个网站时,可能会被封禁。你可以使用以下策略避免这种情况:
- 设置User-Agent:伪装成真正的浏览器。
 - 设置延迟:在连续的请求之间设置延迟,例如使用
time.sleep(5)延迟5秒。 - 使用代理:使用不同的IP地址来发送请求。
 
完整代码示例:
import requests
from bs4 import BeautifulSoup
import time
url = 'https://example.com/articles'  # 换成你的网站
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
try:
    response = requests.get(url, headers=headers, timeout=10)
    response.raise_for_status()
    soup = BeautifulSoup(response.content, 'html.parser')
    articles = soup.find_all('div', class_='article')
    for article in articles:
        title = article.find('h2').text
        link = article.find('a')['href']
        print(title, link)
        time.sleep(5)  # 每抓取一个页面后,暂停5秒
except requests.RequestException as e:
    print(f"Error fetching the url: {url}. Reason: {e}")
 
注意事项
注意:在运行爬虫之前,你应该:
- 检查目标网站的
robots.txt文件,了解哪些页面允许爬取。 - 不要频繁地请求网站,这可能会被视为DDoS攻击。
 - 确保你有权爬取和使用目标数据。
 - 考虑网站的负载,不要对网站造成不必要的压力。
 - 有时候,使用API是获取数据的更好方法,许多网站提供API来获取数据,而不是直接爬取。
 
本期好书推荐《Python网络爬虫入门实战》 
购买链接 
京东:https://item.jd.com/14049708.html 













![2023年中国云存储优势、产值及市场规模分析[图]](https://img-blog.csdnimg.cn/img_convert/98f3228ef22b3e7bee39e15bc71dcc4b.png)



![[C++随想录] 模版进阶](https://img-blog.csdnimg.cn/62074036f0fd4c43a5cd2985534efcd4.png)


