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

本文共 1424 字,大约阅读时间需要 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:        print(news.select(".news-list-title"))for news in soup.select("li"):    if len(news.select(".news-list-title")) > 0:        t=news.select('.news-list-title')[0].text        dt=news.select('.news-list-info')[0].contents[0].text        a = news.select('a')[0].attrs['href']        print(t,dt,a,'\n')for news in soup.select("li"):    if len(news.select(".news-list-title")) > 0:        a = news.select('a')[0].attrs['href']#新闻链接        t = news.select('.news-list-title')[0].text#标题        res1=requests.get(a)        res1.encoding='utf-8'        soup1=BeautifulSoup(res1.text,'html.parser')        dt=soup1.select('.show-info')[0].text#时间        print(t,'\n',dt,'\n')        print(soup1.select('#content')[0].text)#内容        t1=dt.lstrip('发布时间:')[:19]        i=dt.find('摄影:')        s=dt[dt.find('来源:'):].split()[0].lstrip('来源:')        a = dt[dt.find('作者:'):].split()[0].lstrip('作者:')        if i>0:            p = dt[dt.find('摄影:'):].split()[0].lstrip('摄影:')            print(t1,a,s,p)        breakstr = '2018-03-30 17:10:12 'datetime.strptime(str,'%Y-%m-%d %H:%M:%S ')print('\n',str)

转载于:https://www.cnblogs.com/zhichong/p/8695278.html

你可能感兴趣的文章
windows端运行.sh脚本
查看>>
贪心+stack Codeforces Beta Round #5 C. Longest Regular Bracket Sequence
查看>>
泛型中去掉指定字段重复的数据
查看>>
iptables 指南
查看>>
Oracle 11g远程连接 全解 转载
查看>>
shell替换一个或多个空格为逗号
查看>>
懒加载
查看>>
Spring笔记2
查看>>
[翻译] 如何让您的网站真正快
查看>>
递归函数
查看>>
多行显示时,最后一行超出的部分隐藏并显示省略号
查看>>
Android将指定的.class打包到mainDex中
查看>>
Sql Server 2016新功能之 Row-Level Security
查看>>
安装模块的方法
查看>>
ORACLE telnet 1521 不通及ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务的解决...
查看>>
logging 模块使用
查看>>
fastjson序列化排序问题
查看>>
AcDream 1080 面面数 递推Or待定系数法
查看>>
程序包org.apache.commons.beanutils不存在
查看>>
创建数据库的方法
查看>>