作业来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/3002

0.从新闻url获取点击次数,并整理成函数

  • newsUrl
  • newsId(re.search())
  • clickUrl(str.format())
  • requests.get(clickUrl)
  • re.search()/.split()
  • str.lstrip(),str.rstrip()
  • int
  • 整理成函数
  • 获取新闻发布时间及类型转换也整理成函数
import re
url='http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0320/11029.html'
clickurl='http://oa.gzcc.cn/api.php?op=count&id=11029&modelid=80'
re.match('http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0320/(.*).html',url)
re.match('http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0320/(.*).html',url).groups(0)
re.search('/(\d*).html',url).groups(1)
re.findall('(\d+)',url)

结果如下:

1.从新闻url获取新闻详情: 字典,anews

import requests
from bs4 import BeautifulSoup
from datetime import datetime
import re def click(url):
id=re.findall('(\d{1,5})',url)[-1]
clickUrl = 'http://oa.gzcc.cn/api.php?op=count&id=11029&modelid=80'.format(id)
resClick = requests.get(clickUrl)
newsClick = int(resClick.text.split('.html')[-1].lstrip("('").rstrip("');"))
return newsClick def newsdt(showinfo):
newsDate = showinfo.split()[0].split(':')[1]
newsTime = showinfo.split()[1]
newsDT = newsDate+' '+newsTime
dt = datetime.strptime(newsDT, '%Y-%m-%d %H:%M:%S') return dt def anews(url):
newsDetail = {}
res = requests.get(url)
res.encoding ='utf-8'
soup = BeautifulSoup(res.text,'html.parser')
newsDetail['nenewsTitle'] =soup.select('.show-title')[0].text
showinfo = soup.select('.show-info')[0].text
newsDetail['newsDT']=newsdt(showinfo)
newsDetail['newsClick'] =click(newsUrl)
return newsDetail newsUrl='http://news.gzcc.cn/html/2005/xiaoyuanxinwen_0710/4.html'
anews(newsUrl)

结果:

2.从列表页的url获取新闻url:列表append(字典) alist

  1. 获取列表数据
listurl = 'http://news.gzcc.cn/html/xiaoyuanxinwen/'
res = requests.get(listurl)
res.encoding ='utf-8'
soupn = BeautifulSoup(res.text,'html.parser') # a=soupn.select('a')
soupn

2.过滤过滤数据,只获取列表的新闻信息

for news in soupn.select('li'):
if news.select('.news-list-title'):
print(news)
newsUrl = news.a['href']
print(news.a['href'])

3.获取整页信息

def alist(listUrl):
res = requests.get(listurl)
res.encoding ='utf-8'
soup = BeautifulSoup(res.text,'html.parser')
newsList =[]
for news in soupn.select('li'):
if len(news.select('.news-list-title'))>0:
newsUrl = news.select('a')[0]['href']
newsDesc = news.select('.news-list-description')[0].text
newsDict = anews(newsUrl)
newsDict['newsUrl'] = newsUrl
newsDict['description'] = newsDesc
newsList.append(newsDict)
return newsList listUrl = 'http://news.gzcc.cn/html/xiaoyuanxinwen/'
alist(listUrl)

3.生成所页列表页的url并获取全部新闻 :列表extend(列表) allnews

*每个同学爬学号尾数开始的10个列表页

  • 获取多页信息

  • 截取以学号尾数开始的10个列表页
listUrl = 'http://news.gzcc.cn/html/xiaoyuanxinwen/'
allnews = alist(listUrl) for i in range(7,17): #学号为7,截取10页
listUrl = 'http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i)
allnews.extend(alist(listUrl)) len(allnews)

4.设置合理的爬取间隔

import time

import random

time.sleep(random.random()*3)

import time
import random
listUrl = 'http://news.gzcc.cn/html/xiaoyuanxinwen/'
allnews = alist(listUrl) for i in range(1,170): #学号为7,截取10页
listUrl = 'http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i)
allnews.extend(alist(listUrl))
time.sleep(random.random()*3) #设置每3秒爬取一次
print(alist(listUrl)) len(allnews)

  

5.用pandas做简单的数据处理并保存

保存到csv或excel文件

newsdf.to_csv(r'F:\duym\爬虫\gzccnews.csv')

  • 使用pandas函数整理爬取的数据

  • 列表的形式打印数据

  • 显示 “newsClick” 游览次数大于2337的新闻

  • 生成csv文件
newsdf.to_csv(r'E:\gzcc.csv')

菜鸟学IT之python网页爬取多页爬取的更多相关文章

  1. 菜鸟学IT之python网页爬取初体验

    作业来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2881 1. 简单说明爬虫原理 爬虫简单来说就是通过程序模拟浏览器放松请求站 ...

  2. python实现一个栏目的分页抓取列表页抓取

    python实现一个栏目的分页抓取列表页抓取 #!/usr/bin/env python # coding=utf-8 import requests from bs4 import Beautifu ...

  3. 菜鸟学IT之python词云初体验

    作业来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2822 1. 下载一长篇中文小说. 2. 从文件读取待分析文本. txt = ...

  4. python网页爬虫开发之五-反爬

    1.头信息检查是否频繁相同 随机产生一个headers, #user_agent 集合 user_agent_list = [  'Mozilla/5.0 (Windows NT 6.1; WOW64 ...

  5. Python爬虫学习三------requests+BeautifulSoup爬取简单网页

    第一次第一次用MarkDown来写博客,先试试效果吧! 昨天2018俄罗斯世界杯拉开了大幕,作为一个伪球迷,当然也得为世界杯做出一点贡献啦. 于是今天就编写了一个爬虫程序将腾讯新闻下世界杯专题的相关新 ...

  6. 初识python 之 爬虫:使用正则表达式爬取“糗事百科 - 文字版”网页数据

    初识python 之 爬虫:使用正则表达式爬取"古诗文"网页数据 的兄弟篇. 详细代码如下: #!/user/bin env python # author:Simple-Sir ...

  7. 初识python 之 爬虫:使用正则表达式爬取“古诗文”网页数据

    通过requests.re(正则表达式) 爬取"古诗文"网页数据. 详细代码如下: #!/user/bin env python # author:Simple-Sir # tim ...

  8. Python网页解析库:用requests-html爬取网页

    Python网页解析库:用requests-html爬取网页 1. 开始 Python 中可以进行网页解析的库有很多,常见的有 BeautifulSoup 和 lxml 等.在网上玩爬虫的文章通常都是 ...

  9. python使用requests库爬取网页的小实例:爬取京东网页

    爬取京东网页的全代码: #爬取京东页面的全代码 import requests url="https://item.jd.com/2967929.html" try: r=requ ...

随机推荐

  1. Oracle和Mysql获取uuid的方法对比

    场景:orm框架用mybatis,需要往数据库新增一条数据,用Oracle和mysql数据库分别需要怎么实现? mysql方法,用mysql提供的uuid函数 <insert id=" ...

  2. 【Python3爬虫】自动查询天气并实现语音播报

    一.写在前面 之前写过一篇用Python发送天气预报邮件的博客,但是因为要手动输入城市名称,还要打开邮箱才能知道天气情况,这也太麻烦了.于是乎,有了这一篇博客,这次我要做的就是用Python获取本机I ...

  3. 安装Phalcon报错:gcc: Internal error: Killed (program cc1)

    起因 安装Phalcon可以参考github上面的README.md 下面是我在阿里云ECS服务器上面执行命令的过程: # 安装依赖 sudo yum install php-devel pcre-d ...

  4. 【带着canvas去流浪(8)】碰撞

    目录 一. canvas的能力 二. 动画框架 三. 在canvas中模拟碰撞 3.1定义小球的属性 3.2 生成新的小球 3.3 帧动画绘制函数step 3.4 定义小球的update方法 3.5 ...

  5. Unity 虚拟摇杆的实现

    一般地,虚拟摇杆是放在UI层的. 所以先在Canvas建立一个空对象(这里被命名成MoveController),再在空对象里面放一个作为摇杆图片的Image. 然后通过覆盖重写UnityEngine ...

  6. leetcode 链表类型题目解题总结

    最基础的方式要做到非常熟练,要熟练到不思考就能写,但又需明白各处的要求和陷阱 合并两个有序链表的操作,在前面加上一个初始节点,注意while循环和退出时的处理,理解如何处理其中一个链表遍历完的情况 L ...

  7. LeetCode矩阵题型

    以三角形遍历矩阵 ; i < matrix.size(); ++i) { ; j < matrix[i].size(); ++j) swap(matrix[i][j], matrix[j] ...

  8. WebApi中使用Ninject 依赖注入

    之前Ninject依赖注入是在MVC中使用,最近在WebApi中使用,用之前的MVC方式发现使用接口注入,一直是Null错误,网上查询了一些资源,总结一下,以后备用. 主要分为以下几步骤: 在NuGe ...

  9. Linux下安装使用Redis

    cd /usr/local/src   //进入src目录  wget http://download.redis.io/releases/redis-4.0.1.tar.gz  //下载到src   ...

  10. 小tips:使用rem+vw实现简单的移动端适配

    首先设置meta属性,如下代码: <meta name="viewport" content="width=device-width, initial-scale= ...