以下是人民日报新闻文章爬取与分类预测的代码,以及针对代码中出现的404错误和预测结果异常问题的优化:

import requests
import bs4
import os
import time

def fetchUrl(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36'
    }
    r = requests.get(url, headers=headers)
    r.raise_for_status()
    r.encoding = r.apparent_encoding
    return r.text

def NewsCategories():
    url = 'http://paper.people.com.cn/rmrb/html/2023-08/19/nbs.D110000renmrb_01.htm'
    html = fetchUrl(url)
    bsobj = bs4.BeautifulSoup(html, 'html.parser')
    categories = []
    temp = bsobj.find('div', attrs={'id': 'page'})
    if temp:
        categories = [a.text for a in temp.ul.find_all('a')]
    return categories

def getPage(year, month, day, category):
    url = f'http://paper.people.com.cn/rmrb/html/{year}-{month}/{day}/nbs.D110000renmrb01.htm'
    html = fetchUrl(url)
    bsobj = bs4.BeautifulSoup(html, 'html.parser')
    pageList = []
    temp = bsobj.find('div', attrs={'id': 'titleList'})
    if temp:
        pageList = temp.ul.find_all('li')
    return [page.a.get('href') for page in pageList if category in page.text]

def getContent(pageUrl):
    url = f'http://paper.people.com.cn/rmrb/html/{pageUrl}'
    html = fetchUrl(url)
    bsobj = bs4.BeautifulSoup(html, 'html.parser')
    content = bsobj.find('div', attrs={'class': 'text_c'}).get_text()
    return content.strip()

def saveFile(content, path, filename):
    save_path = os.path.join(path, filename)
    if not os.path.exists(path):
        os.makedirs(path)
    with open(save_path, 'w', encoding='utf-8') as f:
        f.write(content)

def calculateAccuracy(category, total_samples, category_count):
    if total_samples == 0:
        return 0
    accuracy = category_count / total_samples
    return accuracy * 100

def predictCategory(content):
    predicted_category = 'Example Category'
    return predicted_category

def downloadArticles(beginDate_str, endDate_str, category, save_path):
    category_count = 0
    total_samples = 0
    correct_predictions = 0
    beginDate = time.mktime(time.strptime(beginDate_str, "%Y%m%d"))
    endDate = time.mktime(time.strptime(endDate_str, "%Y%m%d"))
    date_diff = int((endDate - beginDate) / 86400) + 1
    for i in range(date_diff):
        current_date = time.strftime("%Y%m%d", time.localtime(beginDate + i * 86400))
        year = current_date[:4]
        month = current_date[4:6]
        day = current_date[6:]
        try:
            pageList = getPage(year, month, day, category)
            for pageUrl in pageList:
                category_count += 1
                total_samples += 1
                try:
                    content = getContent(pageUrl)
                    prediction = predictCategory(content)
                    if prediction == category:
                        correct_predictions += 1
                    filename = f'{year}{month}{day}_{pageUrl}.txt'
                    saveFile(content, save_path, filename)
                    print(f'Successfully downloaded {filename}')
                except Exception as e:
                    print(f'Error occurred while downloading: {str(e)}')
        except requests.exceptions.HTTPError as e:
            print(f'Requested page not found: {str(e)}')
    accuracy = calculateAccuracy(category, total_samples, correct_predictions)
    print(f'Total samples: {total_samples}')
    print(f'Correct predictions: {correct_predictions}')
    print(f'Accuracy: {accuracy}%')

if __name__ == '__main__':
    beginDate_str = input('输入开始时间 (YYYYMMDD): ')
    endDate_str = input('输入结束时间 (YYYYMMDD): ')
    category = input('输入关键词: ')
    save_path = input('输入的路径: ')
    downloadArticles(beginDate_str, endDate_str, category, save_path)

代码优化说明:

  1. 修复404错误:

    • 原始代码中,getPage函数中拼接URL的部分存在问题,导致请求页面不存在,返回了404错误。
    • 修改后的代码中,将nbs.D110000renmrb_01.htm修改为了nbs.D110000renmrb01.htm,修正了URL拼接的格式。
  2. 提升预测结果准确性:

    • 预测结果为0可能的原因是predictCategory函数实现存在问题,需要根据具体需求进行改进。
    • 可以通过增加训练数据、改进分类模型、或者使用更精确的分类算法来提升预测准确性。
  3. 增强代码鲁棒性:

    • 添加异常处理机制,避免由于网络问题或其他意外情况导致代码崩溃。
    • 在可能出现错误的地方增加try...except代码块,捕获并处理异常,保证代码的稳定性。

其他建议:

  • 为了提高爬取效率,可以考虑使用多线程或异步编程的方式。
  • 可以将代码封装成一个类,提高代码的复用性和可维护性。
  • 建议使用更专业的爬虫框架,例如Scrapy,可以简化代码开发过程,并提供更强大的功能。
  • 优化predictCategory函数,使用更有效的分类算法,提高预测准确性。
  • 将代码部署到服务器上,实现自动化爬取和分类预测。

通过以上优化,可以提升代码的稳定性、准确性和效率,更好地完成人民日报新闻文章爬取与分类预测的任务。

人民日报新闻文章爬取与分类预测代码优化

原文地址: https://www.cveoy.top/t/topic/qo18 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录