作业来源: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. ansible copy 模块的使用

    copy copy 模块是将 ansible 管理主机上的文件拷贝上远程主机中,与 fetch 相反,如果目标路径不存在,则自动创建,如果 src 的目录带“/” 则复制该目录下的所有东西,如果 sr ...

  2. ES 06 - 通过Kibana插件增删改查ES中的索引文档

    目录 1 document的结构 2 document的常见CRUD操作 2.1 添加商品: 添加文档并建立索引 2.2 查询商品: 检索文档 2.3 修改商品: 替换文档 2.4 修改商品: 更新文 ...

  3. ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)解决办法

    一,报错原因及分析 mysql的这个报错的原因是mysql服务没有正确启动就是mysqld这个程序. mysql要想运行需要mysql和mysqld两个都启动才行 二,解决办法 右键我的电脑——> ...

  4. Spring Boot 2.x(十一):AOP实战--打印接口日志

    接口日志有啥用 在我们日常的开发过程中,我们可以通过接口日志去查看这个接口的一些详细信息.比如客户端的IP,客户端的类型,响应的时间,请求的类型,请求的接口方法等等,我们可以对这些数据进行统计分析,提 ...

  5. Flutter 即学即用系列博客——08 MethodChannel 实现 Flutter 与原生通信

    背景 前面我们讲了很多 Flutter 相关的知识点,但是我们并没有介绍怎样实现 Flutter 与原生的通信. 比如我在 Flutter UI 上面点击了一个按钮,我希望原生做一些处理,那么原生怎么 ...

  6. 用VS2017进行移动开发(C#、VB.NET)——Progress控件,Smobiler移动开发

    Progress控件 一.          样式一 我们要实现上图中的效果,需要如下的操作: 从工具栏上的“Smobiler Components”拖动一个Progress控件到窗体界面上 修改Pr ...

  7. CVE-2017-1000405 利用脏牛漏洞Linux提权复现

    当前路径: /var/www 磁盘列表: / 系统信息: Linux zico 3.2.0-23-generic #36-Ubuntu SMP Tue Apr 10 20:39:51 UTC 2012 ...

  8. Mysql 的 create as 和create like 区别

    大家可能使用Navicat Premium时发现很方便,比如复制表或数据结构等,其实这种复制表数据或结构方法就是create table as 和create table like 这种方式实现细心的 ...

  9. 【译】.NET 跨平台界面框架和为什么你首先要考虑再三

    现在用 C# 来开发跨平台应用已经有很成熟的方案,即共用非界面代码,而每个操作系统搭配特定的用户界面代码.这个方案的好处是可以直接使用操作系统原生的控件和第三方控件,还能够和操作系统深度集成. 这里的 ...

  10. vue项目中获取cdn域名插件

    import axios from 'axios' let CdnPath = {} CdnPath.install = function (Vue, options) { Vue.prototype ...