python3爬虫(未完结)

一个简单的例子:爬取自己的csdn博客,统计每篇博客的访问量,制作一个柱状图,以访问量从大到小的方式显示。

1. 首先从"个人主页"爬取所有所有文章的链接

1.1 打开个人主页,右键->检查:可以看到每篇文章的链接挂在哪个标签的哪个属性下( <article>标签下的<a>标签中的href属性值即为每篇文章的链接 )

1.2 代码提取网页中的所有文章ip(我们可以发现,当页面内容过多时,需要下拉"加载",才能显示所有内容,所以这里需要一个工具模拟浏览器行为,自动滚动页面以加载更多内容。待完善)

python 复制代码
from bs4 import BeautifulSoup  #pip3 install beautifulsoup4
from urllib.request import urlopen

homePage_url="your_blog_link"  #你的csdn个人主页链接
homePage_html=urlopen(homePage_url).read().decode('utf-8')
soup=BeautifulSoup(homePage_html,features='lxml')

#1.查找所有的<article>标签
li_articles=soup.find_all('article')

#2.取出所有<article>标签下<a>中的href属性值
article_urls=[]
for item in li_articles:
    link=item.find_all('a')
    article_urls.append(link[0]['href'])
    print(link[0]['href'])

1.3 结果如下:

相关推荐
金銀銅鐵12 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf13 小时前
Agent 流程编排
后端·python·agent
copyer_xyf13 小时前
Agent RAG
后端·python·agent
copyer_xyf13 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf14 小时前
Agent 记忆管理
后端·python·agent
星云穿梭1 天前
用Python写一个带图形界面的学生管理系统——完整教程
python
金銀銅鐵1 天前
用 Pygame 实现 15 puzzle
python·数学·游戏
黄忠1 天前
大模型之LangGraph技术体系
python·llm
hboot2 天前
AI工程师第二课 - 数据处理
人工智能·python·数据分析
用户8356290780512 天前
使用 Python 自动化 PowerPoint 形状布局与格式设置
后端·python