Python实现抓取CSDN热门文章列表
来源:互联网 发布:怎样在淘宝上卖东西 编辑:程序博客网 时间:2024/06/16 21:41
1、使用工具:
Python3.5
BeautifulSoup
2、抓取网站:
csdn热门文章列表 http://blog.csdn.net/hot.html
3、分析网站代码:
4、实现代码:
__author__ = 'Administrator'import urllib.requestimport refrom bs4 import BeautifulSoup########################################################## 抓取csdn首页文章http://blog.csdn.net/?&page=1###########################################################class CsdnUtils(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, page=None): soup = self.getPage(url) itemBlog = soup.find_all('div', 'blog_list') cnArticle = CsdnUtils print("========================第", page, "页======================================") for i, itemSingle in enumerate(itemBlog): cnArticle.num = i cnArticle.author = itemSingle.find('a', 'user_name').string cnArticle.postTime = itemSingle.find('span', 'time').string cnArticle.articleView = itemSingle.find('a', 'view').string if itemSingle.find('h1').find('a').has_attr('class'): cnArticle.type = itemSingle.find('h1').find('a', 'category').string else: cnArticle.type = "None" cnArticle.title = itemSingle.find('h1').find('a', attrs={'name': True}).string cnArticle.url = itemSingle.find('h1').find('a', attrs={'name': True}).get("href") print("数据:", cnArticle.num + 1, '\t', cnArticle.author, '\t', cnArticle.postTime, '\t', cnArticle.articleView, '\t', cnArticle.type, '\t', cnArticle.title, '\t', cnArticle.url)####### 执行入口 ########if __name__ == "__main__": #要抓取的网页地址'http://blog.csdn.net/?&page={}'.format(i+1),i+1) url = "http://blog.csdn.net/hot.html" cnblog = CsdnUtils() for i in range(0, 5): cnblog.parsePage(url, i + 1)
5、执行结果:
0 0
- Python实现抓取CSDN热门文章列表
- Python抓取简书的热门文章
- Python实现抓取CSDN博客首页文章列表
- 热门文章列表
- Python简单抓取CSDN博文列表并写入SQL Server数据库
- python爬虫CSDN文章抓取
- Python爬虫抓取csdn博客
- [Python]抓取豆瓣电影列表的标题
- python爬虫(抓取百度新闻列表)
- 【Python】抓取淘宝列表页信息
- python抓取csdn博客文章信息
- (1)Python笔记:抓取CSDN博文
- Python 实现腾讯新闻抓取
- python多线程实现抓取网页
- 人民日报语料库抓取python实现
- python 日志增量抓取实现
- python实现爬虫抓取段子
- Python 实现腾讯新闻抓取
- 烟花效果
- 错过了,VR BOX就是隔壁老王的了
- Android 2D Xfermode 理解实现特定形状的头像
- jQuery 实现 select模糊查询 反射机制
- DLL注入
- Python实现抓取CSDN热门文章列表
- View与ViewGroup
- Spring 注解
- HDU5327 Olympiad
- [混合开发]HybridApp分析!!!
- 题目1082:代理服务器
- ACM学习历程7——Vector向量容量扩展机制
- [leetcode]79. Word Search -- JavaScript 代码
- WWW 服务器(BOA)搭建