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

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

1获取单条新闻的#标题#链接#时间#来源#内容 #点击次数,并包装成一个函数。

import requestsimport refrom bs4 import BeautifulSoupurl='http://news.gzcc.cn/html/xiaoyuanxinwen/'res=requests.get(url)res.encoding='utf-8'soup=BeautifulSoup(res.text,'html.parser')

2获取一个新闻列表页的所有新闻的上述详情,并包装成一个函数。

def getclick(newurl):    id=re.search('_(.*).html',newurl).group(1).split('/')[1]    clickurl='http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(id)    click=int(requests.get(clickurl).text.split(".")[-1].lstrip("html('").rstrip("');"))    return click

3获取所有新闻列表页的网址,调用上述函数。

def getonpages(listurl):    res=requests.get(listurl)    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 #标题            time=news.select('.news-list-info')[0].contents[0].text#时间            url1=news.select('a')[0]['href'] #链接            source=news.select('.news-list-info')[0].contents[1].text#来源            description=news.select('.news-list-description')[0].text #内容            resd=requests.get(url1)            resd.encoding='utf-8'            soupd=BeautifulSoup(resd.text,'html.parser')            detail=soupd.select('.show-content')[0].text            click=getclick(url1) #调用点击次数            print(title,url1,click)

4完成所有校园新闻的爬取工作。

count=int(soup.select('.a1')[0].text.rstrip("条"))pages=count//10+1for i in range(2,4):    pagesurl="http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html".format(i)    getonpages(pagesurl)

转载于:https://www.cnblogs.com/diaowen/p/7658252.html

你可能感兴趣的文章
[LeetCode] 675. Cut Off Trees for Golf Event
查看>>
SQLServer之锁简介
查看>>
从点餐小程序说起,谈谈如何从0到1设计一款toB类产品
查看>>
CSS相对定位和绝对定位
查看>>
PHP 协程:Go + Chan + Defer
查看>>
为什么面试完,总是让你回去等通知?
查看>>
断开TCP连接
查看>>
Elasticsearch 参考指南(Avg聚合)
查看>>
利用VisualVm和JMX远程监控K8S里的Java进程
查看>>
Java IO学习一:File类
查看>>
web开发安全框架中的Apache Shiro的应用
查看>>
阿里云周源:一篇文章读懂四代视频加密技术演进
查看>>
容器宽高不确定,图片宽高不确定,css如何实现图片响应式?
查看>>
vue观察模式浅析
查看>>
一只前端小白的JS note
查看>>
React 学习之路 (四) state & 生命周期
查看>>
#2 归并排序算法的简单分析
查看>>
Spring Cloud Gateway的入门案例
查看>>
October CMS - 快速入门 19 - 表单验证
查看>>
我的前端集成测试(一)- 认识node的assert模块
查看>>