爬取校园新闻首页的新闻的详情,使用正则表达式,函数抽离

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

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

3. 将字符串格式的发布时间转换成datetime类型

4. 使用正则表达式取得新闻编号

5. 生成点击次数的Request URL

6. 获取点击次数

7. 将456步骤定义成一个函数 def getClickCount(newsUrl):

8. 将获取新闻详情的代码定义成一个函数 def getNewDetail(newsUrl):

9. 尝试用使用正则表达式分析show info字符串,点击次数字符串。

import requests
from  bs4 import  BeautifulSoup
from datetime import datetime
import locale
import re
locale.setlocale(locale.LC_CTYPE,‘chinese‘)
newsurl = ‘http://news.gzcc.cn/html/xiaoyuanxinwen/‘
res = requests.get(newsurl) #返回response
res.encoding = ‘utf-8‘
soup = BeautifulSoup(res.text,‘html.parser‘)

def getClickCount(newsUrl):   #获取点击次数
    newsId = re.findall(‘\_(.*).html‘, newsUrl)[0].split(‘/‘)[1]   #使用正则表达式取得新闻编号
    clickUrl = ‘http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80‘.format(newsId)
    clickStr = requests.get(clickUrl).text
    return(re.search("hits‘\).html\(‘(.*)‘\);",clickStr).group(1))

def getNewDetail(newsUrl):   #获取新闻详情
    resd = requests.get(newsUrl)  # 返回response
    resd.encoding = ‘utf-8‘
    soupd = BeautifulSoup(resd.text, ‘html.parser‘)
    print(‘标题:‘ + title)
    print(‘描述:‘ + description)
    print(‘链接:‘ + newsUrl)
    info = soupd.select(‘.show-info‘)[0].text
    time = re.search(‘发布时间:(.*) \xa0\xa0 \xa0\xa0作者:‘, info).group(1)
    dtime = datetime.strptime(time, ‘%Y-%m-%d %H:%M:%S‘)   #时间转换成datetime类型
    print(‘发布时间:{}‘.format(dtime))
    print(‘作者:‘ + re.search(‘作者:(.*)审核:‘, info).group(1))
    print(‘审核:‘ + re.search(‘审核:(.*)来源:‘, info).group(1))
    print(‘来源:‘ + re.search(‘来源:(.*)摄影:‘, info).group(1))
    print(‘摄影:‘ + re.search(‘摄影:(.*)点击‘, info).group(1))
    print(‘点击次数:‘ + getClickCount(a))
    print(‘正文:‘+soupd.select(‘.show-content‘)[0].text)

for news in soup.select(‘li‘):
    if len(news.select(‘.news-list-title‘))>0:
        title = news.select(‘.news-list-title‘)[0].text
        description = news.select(‘.news-list-description‘)[0].text
        a = news.a.attrs[‘href‘]
        getNewDetail(a)
        break

原文地址:https://www.cnblogs.com/lawliet12/p/8747187.html

时间: 2024-08-29 14:22:12

爬取校园新闻首页的新闻的详情,使用正则表达式,函数抽离的相关文章

使用scrapy爬虫,爬取今日头条首页推荐新闻(scrapy+selenium+PhantomJS)

爬取今日头条https://www.toutiao.com/首页推荐的新闻,打开网址得到如下界面 查看源代码你会发现 全是js代码,说明今日头条的内容是通过js动态生成的. 用火狐浏览器F12查看得知 得到了今日头条的推荐新闻的接口地址:https://www.toutiao.com/api/pc/focus/ 单独访问这个地址得到 此接口得到的数据格式为json数据 我们用scrapy+selenium+PhantomJS的方式获取今日头条推荐的内容 下面是是scrapy中最核心的代码,位于s

爬取校园新闻

1. 用requests库和BeautifulSoup库,爬取校园新闻首页新闻的标题.链接.正文.show-info. 2. 分析info字符串,获取每篇新闻的发布时间,作者,来源,摄影等信息. 3. 将字符串格式的发布时间转换成datetime类型 4. 使用正则表达式取得新闻编号 5. 生成点击次数的Request URL 6. 获取点击次数 7. 将456步骤定义成一个函数 def getClickCount(newsUrl): 8. 将获取新闻详情的代码定义成一个函数 def getNe

(原)python爬虫入门(2)---排序爬取的辽宁科技大学热点新闻

发现科大网页的源码中还有文章的点击率,何不做一个文章点击率的降序排行.简单,前面入门(1)基本已经完成我们所要的功能了,本篇我们仅仅需要添加:一个通过正则获取文章点击率的数字:再加一个根据该数字的插入排序.ok,大功告成! 简单说一下本文插入排序的第一个循环,找到列表中最大的数,放到列表 0 的位置做观察哨. 上代码: # -*- coding: utf-8 -*- # 程序:爬取点击排名前十的科大热点新闻 # 版本:0.1 # 时间:2014.06.30 # 语言:python 2.7 #--

使用进程池模拟多进程爬取url获取数据,使用进程绑定的回调函数去处理数据

1 # 使用requests请求网页,爬取网页的内容 2 3 # 模拟使用进程池模拟多进程爬取网页获取数据,使用进程绑定的回调函数去处理数据 4 5 import requests 6 from multiprocessing import Pool 7 8 # response = requests.get('http://www.baidu.com') # 访问网页获取网页内容,得到一个网页内容的结果对象<Response [200]>这个200是一个状态码,200表示网页正常的返回,40

使用Python爬取腾讯房产的新闻,用的Python库:requests 、re、time、BeautifulSoup ????

import requests import re import time from bs4 import BeautifulSoup today = time.strftime('%Y-%m-%d',time.localtime(time.time())) one_url = 'http://hz.house.qq.com' #用来构建新的URL的链接 url = 'http://hz.house.qq.com/zxlist/bdxw.htm' #需要爬取的网址 html = requests

爬取IT之家业界新闻

爬取站点  https://it.ithome.com/ityejie/ ,进入详情页提取内容. 1 import requests 2 import json 3 from lxml import etree 4 from pymongo import MongoClient 5 6 url = 'https://it.ithome.com/ithome/getajaxdata.aspx' 7 headers = { 8 'authority': 'it.ithome.com', 9 'met

关于爬取新浪首页显示乱码的解决办法

爬取新浪的主页面,想采集主要页面的新闻板块的主要条目 import requests from bs4 import BeautifulSoup import re html = requests.get("https://www.sina.com.cn") bsobj = BeautifulSoup(html.text) title=bsobj.find_all("a",{"href":re.compile("https://news

网站爬取-案例二:天猫爬取( 第一卷:首页数据抓取)

说到网站数据的爬取,目前为止我见过最复杂的就是天猫了,现在我想对它进行整站的爬取 我们先来看下天猫主页的界面 天猫页面很明显是动态页面 所以我们需要用selenium模块 首先我们抓取下行业列表,留作之后的深度爬取 我们来看下结果: 看到商品链接和行业列表的完美展现了吧 可是当前页面并没抓取完毕,我们现在看下首页还有什么内容 我们顺带抓取下发先并没有我们想要的东西,说明页面没有抓取完毕,熟悉网站制作的同僚们因该知道这样的页面都是用OVERFLOW:hidden的方式来做的布局,所以我们可以利用J

python爬取豆瓣电影首页超链接

什么是爬虫?  我们可以把互联网比作一张大网,而爬虫(即网络爬虫)便是在网上爬行的蜘蛛.把网的节点比作一个个网页,爬虫爬到这就相当于访问了该页面,获取了其信息.可以把节点间的连线比作网页与网页之间的链接关系,这样蜘蛛通过一个节点后,可以顺着节点连线继续爬行到达下一个节点,即通过一个网页继续获取后续的网页,这样整个网的节点便可以被蜘蛛全部爬行到,网站的数据就可以被抓取下来了. 代码: 1 from urllib import request 2 from collections import de