python爬虫原理和编程实战:爬取CSDN博主的账号信息

news/2024/9/24 13:21:58/

🧑 作者简介:阿里巴巴嵌入式技术专家,深耕嵌入式+人工智能领域,具备多年的嵌入式硬件产品研发管理经验。

📒 博客介绍:分享嵌入式开发领域的相关知识、经验、思考和感悟,欢迎关注。提供嵌入式方向的学习指导、简历面试辅导、技术架构设计优化、开发外包等服务,有需要可私信联系。

python爬虫原理和编程实战:爬取CSDN博主的账号信息

  • 1. 爬虫基础知识
  • 2. Python爬虫流程
    • 2.1 发送请求
    • 2.1 获取响应
    • 2.3 解析数据
    • 2.4 存储数据
    • 2.5 处理异常和反爬虫机制
  • 3. python爬虫工具
    • 3.1 requests
    • 3.2 BeautifulSoup
    • 3.3 lxml
    • 3.4 PyQuery
    • 3.5 Scrapy
  • 4. Python爬虫的应用场景
    • 4.1 数据采集和分析
    • 4.2 SEO优化
    • 4.3 舆情监控
    • 4.4 价格监测
  • 5. python爬虫:爬取CSDN博主的账号信息
  • 6. 爬虫的常见问题
    • 6.1 IP限制和封禁
    • 6.2 验证码挑战
    • 6.3 数据结构和内容变化
    • 6.4 遵守法律法规和网站政策
    • 6.5 爬虫性能和效率
    • 6.6 日志记录和错误处理
  • 7. 总结

1. 爬虫基础知识

在这里插入图片描述

1.1 什么是爬虫

爬虫,又称网络爬虫或网络蜘蛛,是一种可以自动化地获取互联网信息的程序或脚本。它可以模拟人的行为,自动浏览网页、提取数据、甚至执行一些简单的操作。爬虫的工作原理是通过自动发送HTTP请求,获取网页内容,然后解析网页数据并进行处理。

1.2 为什么需要爬虫

在当今互联网时代,网络上充满了各种各样的信息,包括新闻、商品信息、股票数据、天气预报等。通过使用爬虫技术,我们能够自动化地获取这些信息,而不需要手动浏览每个网页,从而节省时间和成本。爬虫还可以用于数据分析、搜索引擎优化、舆情监控等各种应用场景,对于个人和企业而言,具有非常重要的意义。

2. Python爬虫流程

2.1 发送请求

爬虫首先向目标网站发送请求。这通常是通过HTTP或HTTPS协议完成的。发送请求时,我们可能还需要处理一些参数,如请求头、请求体、cookies等,以便模拟真实用户的浏览行为。

2.1 获取响应

服务器在接收到请求后,会返回响应。响应中包含了服务器返回的所有数据,这些数据通常是以HTML、JSON或其他格式编码的。

2.3 解析数据

获取到响应数据后,我们需要对其进行解析,以提取我们感兴趣的信息。这通常涉及到对HTML或JSON数据的解析。

2.4 存储数据

解析出数据后,我们可能需要将这些数据存储起来,以便后续的分析或处理。

2.5 处理异常和反爬虫机制

爬虫的运行过程中,可能会遇到各种异常,如网络问题、服务器错误等。此外,很多网站为了防止爬虫,会设置一些反爬虫机制,如验证码、请求频率限制等。因此,我们在编写爬虫时,需要妥善处理这些异常和反爬虫机制,以保证爬虫的稳定性和可靠性。

python_25">3. python爬虫工具

3.1 requests

requests是Python中最受欢迎的HTTP库之一,它提供了简洁易用的API,可用于发送HTTP请求和处理响应。

3.2 BeautifulSoup

BeautifulSoup是一个优秀的HTML和XML解析库,它能够提供简单明了的API和丰富的功能,用于网页信息的提取和解析。

3.3 lxml

lxml是一个高性能的XML和HTML解析库,基于libxml2和libxslt库,可以高效地处理大型文档,适用于需求复杂的网页信息提取。

3.4 PyQuery

PyQuery是一个类似于jQuery的解析库,使用jQuery风格的语法来解析HTML文档,提取所需的数据。

3.5 Scrapy

Scrapy是一个功能强大、灵活的爬虫框架,适用于快速开发爬虫应用,支持数据的抓取、处理和存储。

4. Python爬虫的应用场景

Python爬虫技术具有广泛的应用场景,以下是一些常见的应用领域:

4.1 数据采集和分析

爬虫在数据采集和分析领域有着广泛的应用。企业可以利用爬虫技术从互联网上收集各种数据,如市场行情、竞争对手的动向、产品信息等,用于商业数据分析、市场调研等。同时,爬虫还可以用于科学研究、舆情分析等领域,为数据分析提供更多的信息来源。

4.2 SEO优化

搜索引擎优化(SEO)是一种重要的网络营销手段,而爬虫可以用于获取各种有关网站的数据,如收录情况、关键词排名等,帮助网站优化其搜索引擎排名。通过爬虫技术,可以及时了解搜索引擎对网页的抓取情况,优化网站结构和内容,提高网站在搜索结果中的排名。

4.3 舆情监控

舆情监控是企业常用的一种市场调研手段,通过对社交媒体、新闻网站等信息源进行监控和分析,了解公众对企业、产品或服务的舆论趋势。爬虫可以帮助企业及时获取各种网络信息,并进行分析和汇总,快速了解公众对企业的看法,及时处理负面舆情,制定合适的品牌营销策略。

4.4 价格监测

在电商行业,价格是消费者购买产品时非常重要的考量因素。企业可以利用爬虫技术监测竞争对手的价格变化,也可以根据市场行情进行实时调整,以更好地制定价格和促销策略。而消费者也可以利用爬虫技术来监测商品价格的变动,以获取最优惠的购买时机。

pythonCSDN_59">5. python爬虫:爬取CSDN博主的账号信息

如下所示,我们使用requestsBeautifulSoup两个库开发了一个获取CSDN博主的账号信息的爬虫程序。

python">import requests
from bs4 import BeautifulSoup
import sys
from datetime import datetime
from urllib.parse import urlparsedef make_request(url):iple-quoted-string string">"""发送HTTP请求到指定的URL,并返回响应内容。"""try:headers = {'User-Agent': ('Mozilla/5.0 (Windows NT 10.0; Win64; x64) ''AppleWebKit/537.36 (KHTML, like Gecko) ''Chrome/88.0.4324.150 Safari/537.36')}response = requests.get(url, headers=headers)response.raise_for_status()return responseexcept requests.exceptions.HTTPError as errh:print ("Http Error:", errh)except requests.exceptions.ConnectionError as errc:print ("Error Connecting:", errc)except requests.exceptions.Timeout as errt:print ("Timeout Error:", errt)except requests.exceptions.RequestException as err:print ("Oops: Something Else", err)return Nonedef parse_html(html_content):iple-quoted-string string">"""解析HTML内容并返回BeautifulSoup对象。"""return BeautifulSoup(html_content, 'html.parser')def get_user_id_from_url(url):iple-quoted-string string">"""从URL中提取用户ID"""parse_result = urlparse(url)# 假设用户ID是URL路径的最后一部分user_id = parse_result.path.rstrip('/').split('/')[-1]return user_iddef get_user_nickname(soup):iple-quoted-string string">"""根据提供的BeautifulSoup对象,提取并返回用户昵称。"""nickname_selector = "#userSkin > div.user-profile-head > div.user-profile-head-info > div.user-profile-head-info-t > div > div.user-profile-head-info-rr > div.user-profile-head-info-r-t > div.user-profile-head-name > div:nth-child(1)"nickname_element = soup.select_one(nickname_selector)return nickname_element.get_text(strip=True) if nickname_element else "未知"def get_fans_count(soup):iple-quoted-string string">"""根据提供的BeautifulSoup对象,提取并返回粉丝数量。"""fans_selector = "#userSkin > div.user-profile-head > div.user-profile-head-info > div.user-profile-head-info-t > div > div.user-profile-head-info-rr > div.user-profile-head-info-r-c > ul > li:nth-child(4) > a > div.user-profile-statistics-num"fans_element = soup.select_one(fans_selector)fans_text = fans_element.get_text(strip=True) if fans_element else "0"# 移除数字中的逗号return fans_text.replace(",", "")def get_user_rank(soup):iple-quoted-string string">"""根据提供的BeautifulSoup对象,提取并返回用户排名。"""rank_selector = "#userSkin > div.user-profile-head > div.user-profile-head-info > div.user-profile-head-info-t > div > div.user-profile-head-info-rr > div.user-profile-head-info-r-c > ul > li:nth-child(3) > a > div.user-profile-statistics-num"rank_element = soup.select_one(rank_selector)rank_text = rank_element.get_text(strip=True) if rank_element else "0"# 移除数字中的逗号return rank_text.replace(",", "")def get_personal_introduction(soup):iple-quoted-string string">"""根据提供的BeautifulSoup对象,提取并返回个人简介内容。"""intro_selector = "p.introduction-fold.default"intro_element = soup.select_one(intro_selector)if intro_element:# 获取 <p> 标签下的所有文本intro_text = intro_element.get_text(strip=True)# 将 <span> 标签里的文本 "个人简介:" 移除intro_text = intro_text.replace('个人简介:', '', 1).strip()return intro_textreturn "未知"def get_user_coding_age(soup):iple-quoted-string string">"""根据提供的BeautifulSoup对象,提取并返回用户的码龄(编程年限),不包含 '码龄' 二字。"""coding_age_selector = "#userSkin > div.user-profile-head > div.user-profile-head-info > div.user-profile-head-info-t > div > div.user-profile-head-info-rr > div.user-profile-head-info-r-t > div.user-profile-head-name > div.person-code-age > span"coding_age_element = soup.select_one(coding_age_selector)if coding_age_element:coding_age_text = coding_age_element.get_text(strip=True)# 移除 "码龄" 二字coding_age_text = coding_age_text.replace("码龄", "").strip()return coding_age_textreturn "未知"def get_blog_level(soup):iple-quoted-string string">"""根据提供的BeautifulSoup对象,提取并返回用户的博客等级。"""blog_level_selector = ".user-profile-icon img[src*='blog']"blog_level_element = soup.select_one(blog_level_selector)if blog_level_element and 'src' in blog_level_element.attrs:src = blog_level_element['src']# 提取 src 中 "blog" 后的直到 ".png" 前的数字,作为等级level = src.split('blog')[-1].split('.png')[0]return levelreturn "未知"def get_yuanli_level(soup):iple-quoted-string string">"""根据提供的BeautifulSoup对象,提取并返回用户的原力等级。"""yuanli_level_selector = "#userSkin > div.user-profile-body > div > div.user-profile-body-left > div > div.user-influence-list.user-profile-aside-common-box > ul > li > div.influence-bottom-box > div > div > dl:nth-child(1) > dt"yuanli_level_element = soup.select_one(yuanli_level_selector)return yuanli_level_element.get_text(strip=True) if yuanli_level_element else "未知"def get_registration_date(soup):iple-quoted-string string">"""根据提供的BeautifulSoup对象,提取并返回用户的注册时间。"""registration_date_selector = "#userSkin > div.user-profile-head > div.user-profile-head-info > div.user-profile-head-info-b > div.user-profile-head-info-b-r > div > ul > li.user-general-info-join-csdn > span.user-general-info-key-word"registration_date_element = soup.select_one(registration_date_selector)return registration_date_element.get_text(strip=True) if registration_date_element else "未知"def get_graduate_school(soup):iple-quoted-string string">"""根据提供的BeautifulSoup对象,提取并返回用户的毕业院校信息。"""graduate_school_selector = "#userSkin > div.user-profile-head > div.user-profile-head-info > div.user-profile-head-info-b > div.user-profile-head-info-b-r > div > ul > li.user-general-info-edu > div > span.user-general-info-key-word"graduate_school_element = soup.select_one(graduate_school_selector)return graduate_school_element.get_text(strip=True) if graduate_school_element else "未知"def get_registration_days(soup):iple-quoted-string string">"""计算从注册日期到今天共多少天。"""registration_date_selector = "#userSkin > div.user-profile-head > div.user-profile-head-info > div.user-profile-head-info-b > div.user-profile-head-info-b-r > div > ul > li.user-general-info-join-csdn > span.user-general-info-key-word"registration_date_element = soup.select_one(registration_date_selector)if registration_date_element:date_text = registration_date_element.get_text(strip=True)registration_date = datetime.strptime(date_text, '%Y-%m-%d')today = datetime.now()days_registered = (today - registration_date).daysreturn days_registeredreturn "未知"def convert_days_to_ymd(days):iple-quoted-string string">"""将天数转换成年月日的字符串形式"""years = days // 365days -= years * 365months = days // 30days -= months * 30result = []if years > 0:result.append(f"{years}年")if months > 0:result.append(f"{months}月")if days > 0:result.append(f"{days}天")return "".join(result) if result else "0天"def get_blog_description(soup):iple-quoted-string string">"""根据提供的BeautifulSoup对象,提取并返回博客的描述信息。"""blog_description_selector = "#userSkin > div.user-profile-head > div.user-profile-head-info > div.user-profile-head-info-b > div.user-profile-head-info-b-r > div > div > div:nth-child(2) > div"blog_description_element = soup.select_one(blog_description_selector)return blog_description_element.get_text(strip=True) if blog_description_element else "未知"def get_published_articles_count(soup):iple-quoted-string string">"""根据提供的BeautifulSoup对象,提取并返回已发表文章的数量。"""articles_count_selector = "#userSkin > div.user-profile-head > div.user-profile-head-info > div.user-profile-head-info-t > div > div.user-profile-head-info-rr > div.user-profile-head-info-r-c > ul > li:nth-child(2) > a > div.user-profile-statistics-num"articles_count_element = soup.select_one(articles_count_selector)return articles_count_element.get_text(strip=True) if articles_count_element else "未知"# 主函数,执行脚本逻辑
def main(url):response = make_request(url)if response and response.text:# 请求网页soup = parse_html(response.text)# 获取用户IDuser_id = get_user_id_from_url(url)print(f"用户ID:{user_id}")# 获取用户昵称user_nickname = get_user_nickname(soup)print(f"用户昵称:{user_nickname}")# 获取个人简介personal_introduction = get_personal_introduction(soup)print(f"个人简介:{personal_introduction}")# 获取博客描述信息blog_description = get_blog_description(soup)print(f"博客描述:{blog_description}")# 获取毕业院校信息graduate_school = get_graduate_school(soup)print(f"毕业院校:{graduate_school}")# 获取码龄和注册时间# 获取码龄coding_age = get_user_coding_age(soup)# 获取注册时间registration_date = get_registration_date(soup)# 获取注册天数days_registered = get_registration_days(soup)# 注册时间转换为易读格式ymd_registered = convert_days_to_ymd(days_registered)print(f"注册时间:{registration_date}, 已注册{days_registered}天({ymd_registered}), 码龄:{coding_age}")# 获取博客等级, 原力等级blog_level = get_blog_level(soup)yuanli_level = get_yuanli_level(soup)print(f"博客等级:{blog_level}级,原力等级:{yuanli_level}级")# 获取已发表文章数articles_count = get_published_articles_count(soup)print(f"已发表文章数:{articles_count}")# 获取粉丝数量fans_count = get_fans_count(soup)print(f"粉丝数量:{fans_count}")# 获取用户排名user_rank = get_user_rank(soup)print(f"用户排名:{user_rank}")else:print("请求失败,请检查网页URL地址。")if __name__ == "__main__":# 获取命令行参数if len(sys.argv) < 2:print("Usage: python script.py <URL>")sys.exit(1)input_url = sys.argv[1]main(input_url)

演示:

$ python3 spider_csdn_userinfo.py https://blog.csdn.net/g310773517
用户ID:g310773517
用户昵称:I'mAlex
个人简介:深耕嵌入式+人工智能领域,阿里巴巴嵌入式技术专家。分享嵌入式开发领域的知识、工作过程中的思考、人生的感悟。提供嵌入式方向的学习指导和简历面试辅导,有需要可私信联系。
博客描述:科技改变人类,技术成就未来
毕业院校:未知
注册时间:2010-03-17, 已注册5145天(14年1月5天), 码龄:14年
博客等级:6级,原力等级:5级
已发表文章数:51
粉丝数量:6948
用户排名:3061

6. 爬虫的常见问题

Python爬虫在运行时确实需要注意一系列问题,在爬虫开发过程中着重注意。

6.1 IP限制和封禁

问题描述:许多网站都会采取反爬虫措施,其中最常见的就是检测并限制来自特定IP地址的请求频率。如果爬虫发送的请求过于频繁,服务器可能会暂时或永久封禁该IP地址。

解决方案

  • 使用代理IP:通过轮换使用多个代理IP地址,可以有效避免单一IP被封禁的问题。在爬虫商用过程中,使用IP池代理商是个不错的选择。
    在这里插入图片描述

  • 控制请求频率:合理设置请求之间的间隔时间,避免对服务器造成过大的压力。

  • 遵守robots.txt:确保爬虫遵守目标网站的robots.txt文件规定,这有助于减少被封禁的风险。

6.2 验证码挑战

问题描述:有些网站在检测到异常请求时,会要求用户输入验证码以验证身份。这对于自动化爬虫来说是一个挑战。

解决方案

  • 使用第三方服务:有些服务提供自动识别和输入验证码的功能,但这并不是100%可靠的。
  • 人工介入:当爬虫遇到验证码时,可以设计一种机制,让用户手动输入验证码。

6.3 数据结构和内容变化

问题描述:网站的数据结构和内容可能会随着时间的推移而发生变化,这可能导致爬虫失效。

解决方案

  • 定期更新爬虫:定期检查目标网站的结构和内容,并更新爬虫代码以适应这些变化。
  • 使用动态解析方法:尽量避免硬编码特定的元素ID或类名,而是使用相对位置或基于内容的解析方法。

6.4 遵守法律法规和网站政策

问题描述:在爬取数据时,必须遵守相关的法律法规和网站的隐私政策、服务条款等。

解决方案

  • 了解并遵守法律:确保你的爬虫行为符合当地的法律法规,特别是关于数据保护和隐私的法律。
  • 尊重网站政策:仔细阅读并遵守目标网站的隐私政策和服务条款,确保你的爬虫行为符合网站的规定。

6.5 爬虫性能和效率

问题描述爬虫的性能和效率直接影响到数据的获取速度和资源的消耗。

解决方案

  • 优化代码:使用高效的编程技术和算法来优化爬虫代码。
  • 并发和异步处理:利用Python的并发和异步特性,同时处理多个请求,提高爬取效率。
  • 数据库存储:使用数据库来存储爬取的数据,以便后续的分析和处理。

6.6 日志记录和错误处理

问题描述:在爬虫运行过程中,可能会遇到各种错误和异常情况,需要记录并处理这些错误。

解决方案

  • 记录日志:使用Python的日志模块记录爬虫的运行状态和错误信息。
  • 异常处理:在代码中添加异常处理机制,确保爬虫在遇到错误时能够优雅地退出或重试。

7. 总结

Python爬虫技术是一种强大而灵活的工具,能够帮助我们高效地获取互联网上的数据,用于各种实际应用场景。在使用爬虫时,需要注重合法合规,遵守网站的使用政策,避免对网站造成不必要的压力或侵犯隐私等问题。


http://www.ppmy.cn/news/1426623.html

相关文章

第十五届蓝桥杯题解-握手

题目大意&#xff1a;有50个人组成的派对&#xff0c;每个人都要与其他所有人进行握手&#xff0c;但其中有7个人之间互相不握手&#xff0c;求握手多少次&#xff0c;a与b握手即b与a握手 思路&#xff1a;规定前7个人互相不握手&#xff0c;两重for循环暴力就好 代码&#x…

TS学习笔记(数据类型、高级类型、接口、类、继承、重写、修饰符、抽象类)

TS学习笔记(数据类型、高级类型、接口、类、继承、重写、修饰符、抽象类) 1、谈谈你对ts的理解以及与js的区别 是什么&#xff1f; ts是js的超集&#xff0c;支持es6语法&#xff0c;支持面向对象编程的概念 如&#xff1a;类、接口、继承、泛型 特性&#xff1a; –类型批…

【实测】记一次用windows11家庭中文版部署django+vue项目的踩坑之旅

整个过程下来&#xff0c;给我的感觉就和吃了翔一样难受&#xff0c;窒息的感觉&#xff0c;全程没少骂win11的设计者... 因为要调试一些只有在windows才会出现的测试平台bug&#xff0c;于是我耗重金购入了一台顶配windows笔记本。以下是配置&#xff1a; 配置还蛮高的对吧&am…

Rust---#[derive(Debug)]

在 Rust 中,#[derive(Debug)] 宏用于自动为结构体或枚举实现 Debug trait。Debug trait 允许一个类型的实例被格式化为字符串,通常用于调试输出。以下是 #[derive(Debug)] 通常的使用方式: 目录 定义结构体或枚举使用 println! 宏打印调试信息在自定义 Debug 实现中使用定义…

4.20+C语言感想,有趣的思考题,case的省略操作,统计位数,终止循环,break和continue语句, 准备下一篇见。

鹏哥C语言感想 一.高级 这可不是什么煎饼&#xff0c;这种食物叫做蓝莓&#xff0c;俗称苹果。生长在撒哈拉沙漠的雨林地带。因外形酷似企鹅&#xff0c;所以我们又喜欢叫他北极熊。你们这些人&#xff0c;连仙人掌都不知道&#xff0c;就不要乱说他是西瓜好吗&#xff1f;再…

C#笔记之解析不确定具体结构的JSON内容

需求&#xff1a;服务器会传一段json信息过来&#xff0c;这个json信息是不固定的&#xff0c;可能传过来的有一层或者多层数组结构。需要把这些信息全部解析出来&#xff0c;并加以运用。 实现&#xff1a; 最关键的可能就是这个JToken了&#xff0c;这玩意能区分这个value数…

react15升级17问题记录

,当前旧项目主要依赖版本介绍&#xff1a;这里只贴出重要依赖包的旧版本做展示&#xff0c;可以看到版本都相当落后了&#xff0c;升级的话会涉及一些API以及依赖的修改或者弃用 次文章只记录当前项目使用&#xff0c;其他项目不一定通用 {"react": "^15.6.1&q…

“中医显示器”是人体健康监测器

随着科技的进步&#xff0c;现代医学设备已经深入到了人们的日常生活中。然而&#xff0c;在这个过程中&#xff0c;我们不应忘记我们的医学根源&#xff0c;中医。我们将中医的望、闻、问、切四诊与现代科技相结合&#xff0c;通过一系列的传感器和算法将人体的生理状态以数字…