首页 > 编程知识 正文

使用Python抓取新浪新闻项目

时间:2023-11-22 01:45:34 阅读:294731 作者:SJXS

本文将详细介绍如何使用Python编程语言从新浪新闻网站上抓取新闻信息。我们将从多个方面进行阐述,包括数据爬取、数据处理和数据存储等。

一、数据爬取

1、使用Python的requests模块发送HTTP请求,获取新浪新闻网页的内容。

import requests

url = 'http://news.sina.com.cn/'
response = requests.get(url)
print(response.text)

2、通过解析HTML内容,提取新闻标题、作者、时间等重要信息。

from bs4 import BeautifulSoup

html = response.text
soup = BeautifulSoup(html, 'html.parser')
news_list = soup.find_all('a', class_='news-item')

for news in news_list:
    title = news.text
    author = news.find('span', class_='author').text
    time = news.find('span', class_='time').text
    
    print('标题:', title)
    print('作者:', author)
    print('时间:', time)
    print('-------------------------')

3、使用正则表达式对提取的信息进行进一步筛选和处理。

import re

pattern = re.compile(r'd{4}-d{2}-d{2}')
for news in news_list:
    title = news.text
    author = news.find('span', class_='author').text
    time = news.find('span', class_='time').text
    
    if re.search(pattern, time):
        print('标题:', title)
        print('作者:', author)
        print('时间:', time)
        print('-------------------------')

二、数据处理

1、对获取的时间进行格式化处理,转换为Python的datetime对象。

from datetime import datetime

for news in news_list:
    title = news.text
    author = news.find('span', class_='author').text
    time = news.find('span', class_='time').text
    
    # 时间格式化
    datetime_obj = datetime.strptime(time, '%Y-%m-%d')
    print('标题:', title)
    print('作者:', author)
    print('时间:', datetime_obj)
    print('-------------------------')

2、使用Python的pandas库对数据进行进一步处理和分析。

import pandas as pd

data = []
for news in news_list:
    title = news.text
    author = news.find('span', class_='author').text
    time = news.find('span', class_='time').text
    
    data.append({'标题': title, '作者': author, '时间': time})
    
df = pd.DataFrame(data)
print(df.head())

三、数据存储

1、将数据存储到CSV文件中。

df.to_csv('news.csv', index=False, encoding='utf-8')

2、将数据存储到数据库中。

import sqlite3

conn = sqlite3.connect('news.db')
df.to_sql('news', conn, if_exists='replace', index=False)
conn.close()

通过以上代码示例,我们可以使用Python编程语言实现从新浪新闻网站抓取新闻信息,并进行数据处理和存储。这为我们提供了一个便捷且高效的方式来获取和利用新闻数据。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。