Python抓取博客园首页文章列表(带分页)
1、使用工具:
Python3.5
BeautifulSoup
2、抓取网站:
博客园首页文章列表http://www.cnblogs.com
3、分析网站文章结构:
4、实现代码:
__author__ = 'Administrator'
import urllib.request
import re
from bs4 import BeautifulSoup
import time
########################################################
#
# 抓取博客园首页推荐文章列表http://www.cnblogs.com
#
# 鹿伟伟
#
########################################################
class CnblogsUtils(object):
def __init__(self):
user_agent='Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'
self.headers ={'Cache-Control':'max-age=0',
'Connection':'keep-alive',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'User-Agent':user_agent,
}
def getPage(self,url=None):
request=urllib.request.Request(url,headers=self.headers)
response=urllib.request.urlopen(request)
soup=BeautifulSoup(response.read(),"html.parser")
#print(soup.prettify())
return soup
def parsePage(self,url=None,pageNo=None):
soup=self.getPage(url+pageNo)
itemBlog=soup.find_all("div",{"class":"post_item"})
#print(itemBlog.__len__())
#print(itemBlog[10])
blog=CnblogsUtils()
print("+++++++++++++++++++++++++++第",pageNo,"页++++++++++++++++++++++++++++")
for i,blogInfo in enumerate(itemBlog):
#print(blogInfo)
blog.num=i
blog.url=blogInfo.find("h3").find("a").get("href")
blog.title=blogInfo.find("h3").find("a").string
#print("++++++++++",blogInfo.find("div","post_item_foot").contents[2])
#去掉空格strip()
blog.time=blogInfo.find("div","post_item_foot").contents[2].strip()
blog.author=blogInfo.find("div","post_item_foot").find("a").string
print(blog.num+1,"标题:",blog.title,"作者:",blog.author,"详情:",blog.url,blog.time)
####### 执行 ########
if __name__ =="__main__":
#要抓取的网页地址'http://blog.csdn.net/?&page={}'.format(i+1),i+1)
url = "http://www.cnblogs.com/sitehome/p/"
cnblog=CnblogsUtils()
#cnblog.parsePage(url)
for i in range(0,10):
cnblog.parsePage(url,str(i+1))
time.sleep(3)
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
· 为什么AI教师难以实现
· 如何让低于1B参数的小型语言模型实现 100% 的准确率
· AI Agent爆火后,MCP协议为什么如此重要!
· 【译】Visual Studio(v17.13)中新的调试和分析特性
· Draw.io:你可能不知道的「白嫖级」图表绘制神器