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 结果如下:

相关推荐
用户83562907805112 小时前
无需 Office:Python 批量转换 PPT 为图片
后端·python
markfeng814 小时前
Python+Django+H5+MySQL项目搭建
python·django
GinoWi15 小时前
Chapter 2 - Python中的变量和简单的数据类型
python
JordanHaidee15 小时前
Python 中 `if x:` 到底在判断什么?
后端·python
ServBay15 小时前
10分钟彻底终结冗长代码,Python f-string 让你重获编程自由
后端·python
闲云一鹤16 小时前
Python 入门(二)- 使用 FastAPI 快速生成后端 API 接口
python·fastapi
Rockbean17 小时前
用40行代码搭建自己的无服务器OCR
服务器·python·deepseek
曲幽17 小时前
FastAPI + Ollama 实战:搭一个能查天气的AI助手
python·ai·lora·torch·fastapi·web·model·ollama·weatherapi
用户606487671889618 小时前
国内开发者如何接入 Claude API?中转站方案实战指南(Python/Node.js 完整示例)
人工智能·python·api
只与明月听19 小时前
RAG深入学习之Chunk
前端·人工智能·python