博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
爬取校园新闻首页的新闻
阅读量:6868 次
发布时间:2019-06-26

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

1. 用requests库和BeautifulSoup库,爬取校园新闻首页新闻的标题、链接、正文。

url = "http://news.gzcc.cn/html/xiaoyuanxinwen/"res = requests.get(url);res.encoding = "utf-8"soup = BeautifulSoup(res.text,"html.parser");for news in soup.select("li"):    if len(news.select(".news-list-title"))>0:  #排除为空的li        time = news.select(".news-list-info")[0].contents[0].text        title = news.select(".news-list-title")[0].text        description = news.select(".news-list-description")[0].text        a = news.select('a')[0].attrs['href']        detail_res = requests.get(a)        detail_res.encoding = "utf-8"        detail_soup = BeautifulSoup(detail_res.text, "html.parser")        print(detail_soup.select("#content")[0].text)#正文        print(time, title, description, a)        content = detail_soup.select("#content")[0].text        info = detail_soup.select(".show-info")[0].text        date_time = info.lstrip('发布时间:')[:19]        print(info)        break

2. 分析字符串,获取每篇新闻的发布时间,作者,来源,摄影等信息。

info = '发布时间:2018-04-01 11:57:00      作者:陈流芳  审核:权麟春  来源:马克思主义学院      点击:次'detail_time = info.lstrip('发布时间:')[:19]sh = info[info.find("审核"):].split()[0].lstrip('审核:')print(detail_time,sh)

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

# 获取当前的时间now_time = datetime.now();now_time.year# 将字符串转化为时间print(datetime.strptime(date_time,"%Y-%m-%d %H:%M:%S"))# 将时间转化为字符串print(now_time.strftime('%Y\%m\%d'))

4. 将完整的代码及运行结果截图发布在作业上。

import requestsfrom bs4 import BeautifulSoupfrom datetime import datetimeurl = "http://news.gzcc.cn/html/xiaoyuanxinwen/"res = requests.get(url);res.encoding = "utf-8"soup = BeautifulSoup(res.text,"html.parser");for news in soup.select("li"):    if len(news.select(".news-list-title"))>0:  #排除为空的li        time = news.select(".news-list-info")[0].contents[0].text        title = news.select(".news-list-title")[0].text        description = news.select(".news-list-description")[0].text        a = news.select('a')[0].attrs['href']        detail_res = requests.get(a)        detail_res.encoding = "utf-8"        detail_soup = BeautifulSoup(detail_res.text, "html.parser")        print(detail_soup.select("#content")[0].text)#正文        print(time, title, description, a)        content = detail_soup.select("#content")[0].text        info = detail_soup.select(".show-info")[0].text        date_time = info.lstrip('发布时间:')[:19]        print(info)        breakinfo = '发布时间:2018-04-01 11:57:00      作者:陈流芳  审核:权麟春  来源:马克思主义学院      点击:次'detail_time = info.lstrip('发布时间:')[:19]sh = info[info.find("审核"):].split()[0].lstrip('审核:')print(detail_time,sh)# # 多个名字查找作者info1 = '发布时间:2018-04-01 11:57:00      作者:陈流芳 许健杰  审核:权麟春   来源:马克思主义学院    点击:次 'info1 = info1[info1.find("作者"):info1.find('审核:')].lstrip('作者:').split()[1]print(info1)# 获取当前的时间now_time = datetime.now();now_time.year# 将字符串转化为时间print(datetime.strptime(date_time,"%Y-%m-%d %H:%M:%S"))# 将时间转化为字符串print(now_time.strftime('%Y\%m\%d'))

 

 

转载于:https://www.cnblogs.com/2439466501qq/p/8709992.html

你可能感兴趣的文章
java基础总结
查看>>
算法复杂度
查看>>
Jsonlib 属性过滤器
查看>>
List 去重
查看>>
Android性能优化之内存优化练习
查看>>
LeetCode 465: Optimal Account Balance
查看>>
LeetCode – Refresh – Trapping Rain Water
查看>>
通达OA数据库优化方案之_历史数据清理
查看>>
持续集成之③:将代码自动部署至测试环境
查看>>
第三十五课:Ajax详解
查看>>
Python基础之各种推导式玩法
查看>>
poj 3167(KMP+树状数组)
查看>>
C++ 循环队列基本算法实现
查看>>
maven中,dependency 中的 classifier属性
查看>>
51Nod-1011 最大公约数GCD【欧几里得算法】
查看>>
#从零开始学Swift2.0# No.2 运算符和表达式
查看>>
Ubuntu下安装NetBeans步骤和相关问题的解决方法
查看>>
iOS开发UI中懒加载的使用方法
查看>>
online_judge_1107
查看>>
Ubuntu下修改为永久DNS的方法
查看>>