用requests库和BeautifulSoup4库爬取新闻列表
发布日期:2021-08-19 11:10:14 浏览次数:6 分类:技术文章

本文共 1686 字,大约阅读时间需要 5 分钟。

1.用requests库和BeautifulSoup4库,爬取校园新闻列表的时间、标题、链接、来源。

import requestsfrom bs4 import BeautifulSoupnetwork = 'http://news.gzcc.cn/html/jxky/'res = requests.get(network)res.encoding='utf-8'soup = BeautifulSoup(res.text,'html.parser')for news in soup.select('li'):    if len(news.select('.news-list-title'))>0:        title = news.select('.news-list-title')[0].text        url = news.select('a')[0]['href']        time = news.select('.news-list-info')[0].contents[0].text        main = news.select('.news-list-description')[0].text        source = news.select('.news-list-info')[0].contents[1].text        print('链接:{}'.format(url))        print('标题:{}'.format(title))        print('正文:{}'.format(main))        print('时间:{}'.format(time))        print('来源:{}'.format(source))

3.将其中的时间str转换成datetime类型。

import requestsfrom bs4 import BeautifulSoupfrom datetime import datetimenetwork = 'http://news.gzcc.cn/html/jxky/'res = requests.get(network)res.encoding='utf-8'soup = BeautifulSoup(res.text,'html.parser')for news in soup.select('li'):    if len(news.select('.news-list-title'))>0:        title = news.select('.news-list-title')[0].text        url = news.select('a')[0]['href']        time = news.select('.news-list-info')[0].contents[0].text        timed = datetime.strptime(time,'%Y-%m-%d')        main = news.select('.news-list-description')[0].text        source = news.select('.news-list-info')[0].contents[1].text        print('链接:{}'.format(url))        print('标题:{}'.format(title))        print('正文:{}'.format(main))        print('时间:{}'.format(timed))        print('来源:{}'.format(source))

 

4.选一个自己感兴趣的主题,做类似的操作,为“爬取网络数据并进行文本分析”做准备。

 

转载于:https://www.cnblogs.com/OKding/p/7605968.html

转载地址:https://blog.csdn.net/weixin_30924087/article/details/97024651 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:codevs1183 泥泞的道路
下一篇:接下来自己的研究对象

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年03月12日 13时54分42秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

mysql server卸载出错_Mysql卸载问题Start Server卡住报错解决方法 2019-04-21
全国省市区 mysql_2017全国省市区数据库【含三款数据库】 2019-04-21
druid加载MySQL驱动原理_你好,想知道mybatis+druid+jdbc 原理介绍? 2019-04-21
mysql 怎样链接jdbc_jdbc怎么链接mysql数据库 2019-04-21
mysql学生课程表试题_Mysql练习之 学生表、课程表 、教师表、成绩表 50道练习题... 2019-04-21
java exec封装_Java 执行系统命令工具类(commons-exec) 2019-04-21
php sha512解密,PHP加密函数 sha256 sha512 sha256_file() sha512_file() 2019-04-21
mysql里可以用cube吗_sql server的cube操作符使用详解_mysql 2019-04-21
php mysql 图书_使用PHP+MySQL来对图书管理系统进行构建 2019-04-21
单片机c语言 int1,51单片机into、int1中断计数c语言源程序.doc 2019-04-21
c语言课程设计工资管理建库,C语言课程设计工资管理系统参考.doc 2019-04-21
c语言case中途跳出,break语句在switch结构语句中的作用是终止某个case,并跳出switch结构语句。... 2019-04-21
c51写c语言外部ram头文件,C51中访问外部RAM的方法 2019-04-21
51c语言产生随机证书,基于51单片机的随机数产生器设计-LCD1602-KEY-(电路图+程序源码)... 2019-04-21
C语言编写程序计算高考倒计时天数,基于51单片机LCD12864大字符校时万年历带高考倒计时程序... 2019-04-21
c语言打开一个html文件路径,C语言文件处理-C语言文件的打开和关闭 2019-04-21
普职融通信息技术课本C语言,“三步走”扎实推进“普职融通”办学新模式 2019-04-21
Android多个签名,【Android】Android批量重签名 2019-04-21
html unicode编码转换,JS实现的Unicode编码转换操作示例 2019-04-21
html页面角落放动漫人物,L2Dwidget.js L2D网页动画人物添加 2019-04-21