爬取校园新闻首页的新闻

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

#coding=utf-8
import requests
from bs4 import BeautifulSoup

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

# print(soup)

for i in soup.select('li'):
    if len(i.select('.news-list-title'))>0:
        x=i.select('.news-list-title')[0].text
        y=i.select('.news-list-description')[0].text
        z=i.select('.news-list-info')[0].text
        p = i.select('a')[0].attrs['href']
        print(x,y)
        print(z,p)

截图:

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

for i in soup.select('li'):
    if len(i.select('.news-list-title'))>0:
     a=i.select('.news-list-title')[0].text
     b=i.select('.news-list-description')[0].text
     # c=i.a.attrs['href']
     c=i.select('a')[0].attrs['href']
     read=requests.get(c)
     read.encoding='utf-8'
     soupSecond=BeautifulSoup(read.text,'html.parser')
     d=soupSecond.select('#content')[0].text
     print(soupSecond.select('.show-info')[0].text)

截图:

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字符串,点击次数字符串。

#coding=utf-8
import requests
from bs4 import BeautifulSoup
from datetime import datetime
import re
res = requests.get('http://news.gzcc.cn/html/xiaoyuanxinwen/')
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text, 'html.parser')

def getCount(url):
    newsId = re.findall(r'\_(.*).html', url)[0][-4:]     # 获取新闻编号
    # 生成点击次数的Request URL
    clickUrl = 'http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(newsId)
    clickRes = requests.get(clickUrl)
    # 利用正则表达式获取新闻点击次数
    clickCount = int(re.search("hits').html('(.*)');", clickRes.text).group(1))
    return clickCount

#遍历li标签获取新闻内容
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"] #新闻细节

        resd = requests.get(a)
        resd.encoding = 'utf-8'
        soupd = BeautifulSoup(resd.text, 'html.parser')

        # 正则输出结果
        content = soupd.select('.show-content')[0].text
        info = soupd.select('.show-info')[0].text
        count = getCount(a)      # 再次调用getClickCount()获取点击次数
        time = re.search('发布时间:(.*)xa0xa0 xa0xa0作者:', info).group(1)
        author = re.search('作者:(.*)xa0xa0审核:', info).group(1)
        check = re.search('审核:(.*)xa0xa0来源:', info).group(1)
        sources = re.search('来源:(.*)xa0xa0xa0xa0摄影:', info).group(1)
        # 将时间字符串datetime转换为datetime
        thetime= datetime.strptime(time, '%Y-%m-%d %H:%M:%S')
        print('标题' + ': ' + title)
        print('概要' + ': ' + description)
        print('链接' + ': ' + a)
        print('正文' + ' :' + content)
        print('发布时间:{0}
作者:{1}
来源:{2}
点击次数:{5}'.format(thetime, author, sources, count))
        break
原文地址:https://www.cnblogs.com/tiankongyiluozhiwu/p/8717412.html