python爬取校园新闻实训报告

Python实训报告

实训内容:
编写一个新闻资讯抓取程序,抓取页面新闻资讯数据,并保存在本地文本文件中,每条新闻保存一个记事本,记事本动态生成以新闻标题命名。理工学院新闻通知页面:http://oa.nyist.edu.cn/oo.php

 

一.所需基础知识:

  1. request发送网络请求的简单应用
  2. 正则表达式的应用
  3. Os模块中判断文件是否存在的应用
  4. beautiful soup库的应用。
  5. 循环创建文件以及动态生成新闻标题

二.课题设计思路:

 

  1. 任务:

抓取南阳理工学院官网(http://oa.nyist.edu.cn/oo.php)的新闻内容

  1. 实验流程:
  1. 确定抓取目标
  2. 制定抓取规则
  3. 编写/调试抓取代码
  4. 获得抓取数据
  5. 将抓取的数据单独保存到本地文件

三.总结和体会:
本次实验让我深刻的体会到实践出真知,看再多的书与视频都不及自己上手写代码。写代码的过程中会出现各种各样的错误,将错误信息复制粘贴到百度,可以看到许多和自己相同错误的博客,比如beautifulsoup的使用,在用正则表达式来获取a标签下的信息时 , 我对正则表达式的应用并不熟悉。在不断测试中终于获取了。后来使用网页解析器BeautifulSoup,非常容易的去抓取网页信息。

同时本次实验让我对python的运用更加熟悉,也加深了我对python的兴趣。

 

四.实现过程:

1.首先导入 os ,requests , re 模块 ,网页解析器BeautifulSoup

代码:

import requests

import re

import os

from bs4 import BeautifulSoup

2.一个名为response1的response对象接收网页信息。

代码:

url = 'http://oa.nyist.edu.cn/oo.php'

response1 = requests.get(url)

3.输出格式设置为utf-8

代码:

response1.encoding = 'utf-8'

名为html的变量获取该网页的信息

代码:

html = response1.text
  1. 利用BeautifulSoup解析网页 , 抓取a标签下的内容

代码:

soup = BeautifulSoup(html,'html.parser')

links = soup.find_all('a')
  1. 双层for循环利用BeautifulSoup抓取网页信息,第一层for循环,循环官网页面的链接,第二层for循环循环该链接点击后的网页信息。并在该循环下创建以该名命名的文件 , 并将此网页的内容输入进去。

代码:

for ii in links:

    url = "http://oa.nyist.edu.cn/"+ii['href']

    response2 = requests.get(url)

# response2.encoding = 'UTF-8'

    response2.encoding = response2.apparent_encoding

    html = response2.text

    # print(html)

    soup = BeautifulSoup(html,'html.parser')

    links1 = soup.find_all('td')

    for link in links1:

        file_handdle = open(ii.text+'.txt', mode='a',encoding='utf-8')

        file_handdle.write(link.text)

    file_handdle.close()

    if(os.path.exists(ii.text+'.txt')):

        file_handdle = open(ii.text+ '.txt', mode='r', encoding='utf-8')

        content = file_handdle.read()

        print(content)

 以上就是利用python爬取南阳理工学院官网新闻的全部过程

 

 五.项目源代码:

import requests

import re

import os

from bs4 import BeautifulSoup

url = 'http://oa.nyist.edu.cn/oo.php'

response1 = requests.get(url)

response1.encoding = 'utf-8'

html = response1.text

#获取信息

# print(html)

soup = BeautifulSoup(html,'html.parser')

links = soup.find_all('a')



for ii in links:

    url = "http://oa.nyist.edu.cn/"+ii['href']

    response2 = requests.get(url)

# response2.encoding = 'UTF-8'

    response2.encoding = response2.apparent_encoding

    html = response2.text

    # print(html)

    soup = BeautifulSoup(html,'html.parser')

    links1 = soup.find_all('td')

    for link in links1:

        file_handdle = open(ii.text+'.txt', mode='a',encoding='utf-8')

        file_handdle.write(link.text)

    file_handdle.close()

    if(os.path.exists(ii.text+'.txt')):

        file_handdle = open(ii.text+ '.txt', mode='r', encoding='utf-8')

        content = file_handdle.read()

        print(content)

 

 

  • 2
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值