数据采集实践作业2

news/2024/10/19 6:27:49

作业一

1.实验内容

要求:在中国气象网(http://www.weather.com.cn)给定城市集的7日天气预报,并保存在数据库

代码如下:

import requests
import sqlite3
from bs4 import BeautifulSoup# 定义获取天气信息的函数
def get_weather_info(city_id):url_template = 'http://www.weather.com.cn/weather/{}.shtml'complete_url = url_template.format(city_id)headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'}response = requests.get(complete_url, headers=headers)response.encoding = 'utf-8'soup = BeautifulSoup(response.text, 'html.parser')forecast_data = []days = soup.find('ul', class_='t clearfix').find_all('li')for day in days:day_date = day.find('h1').get_text(strip=True)weather_condition = day.find('p', class_='wea').get_text(strip=True)high_temp = day.find('span').get_text(strip=True) if day.find('span') else ''low_temp = day.find('i').get_text(strip=True)temperature = f"{high_temp}/{low_temp}"forecast_data.append((day_date, weather_condition, temperature))return forecast_data# 创建数据库和表格
def initialize_database():connection = sqlite3.connect('weather_data.db')cursor = connection.cursor()cursor.execute('''CREATE TABLE IF NOT EXISTS forecast (record_id INTEGER PRIMARY KEY AUTOINCREMENT,location TEXT,forecast_date TEXT,condition TEXT,temp_range TEXT)''')connection.commit()return connection# 存储天气信息到数据库
def store_weather_data(location, weather_info, connection):cursor = connection.cursor()for day_date, condition, temp in weather_info:cursor.execute("INSERT INTO forecast (location, forecast_date, condition, temp_range) VALUES (?, ?, ?, ?)",(location, day_date, condition, temp))connection.commit()# 打印数据库中的天气信息
def print_weather_data(connection):cursor = connection.cursor()cursor.execute("SELECT * FROM forecast")records = cursor.fetchall()print(f"{'ID':<5} {'城市':<10} {'日期':<15} {'天气状况':<20} {'温度范围':<15}")for record in records:print(f"{record[0]:<5} {record[1]:<10} {record[2]:<15} {record[3]:<20} {record[4]:<15}")def main():# 城市及其对应的代码cities = {'北京': '101010100','上海': '101020100','福州': '101230101','天津': '101030100'}# 初始化数据库连接db_connection = initialize_database()# 获取并存储每个城市的天气信息for city_name, city_id in cities.items():print(f"获取城市 {city_name} ({city_id}) 的天气信息...")weather_info = get_weather_info(city_id)store_weather_data(city_name, weather_info, db_connection)# 打印数据库中的天气信息print_weather_data(db_connection)db_connection.close()if __name__ == '__main__':main()

结果如下:

2.心得体会

​ 写代码时遇到bug:TypeError: 'bool' object is not callable

​ 仔细查找发现是strip=True后面多了一个括号,判断应该是python版本不同导致的写法不同

作业二

1.实验内容

  • 要求:用requests和BeautifulSoup库方法定向爬取股票相关信息,并存储在数据库中。

  • 候选网站:东方财富网:https://www.eastmoney.com/

  • 新浪股票:http://finance.sina.com.cn/stock/

  • 技巧:在谷歌浏览器中进入F12调试模式进行抓包,查找股票列表加载使用的url,并分析api返回的值,并根据所要求的参数可适当更改api的请求参数。根据URL可观察请求的参数f1、f2可获取不同的数值,根据情况可删减请求的参数。

  • 参考链接:https://zhuanlan.zhihu.com/p/50099084

    代码如下:

    import requests
    import re
    import sqlite3def fetch_html(page_num, query_parameters):base_url = ("http://66.push2.eastmoney.com/api/qt/clist/get?""cb=jQuery112409097606620255823_1696662149317&pn=1&pz=20&po=1&np=" + str(page_num) +"&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&wbp2u=|0|0|0|web&" + query_parameters +"&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152&_=1696662149318")response = requests.get(base_url)regex_pattern = "\"diff\":\\[(.*?)\\]"extracted_data = re.findall(regex_pattern, response.text, re.S)return extracted_datadef process_stock_data(query_params, page_num):# Retrieve the JSON data from the APIjson_data = fetch_html(page_num, query_params)stocks_data = json_data[0].split("},")# Create/connect to a SQLite databasedatabase_connection = sqlite3.connect('stock_data.db')db_cursor = database_connection.cursor()# Create the stocks table if it doesn't existdb_cursor.execute('''CREATE TABLE IF NOT EXISTS stock_info (id INTEGER PRIMARY KEY,stock_code TEXT,stock_name TEXT,stock_price REAL,price_change REAL,price_change_percent REAL,volume INTEGER,turnover REAL,amplitude REAL,highest REAL,lowest REAL,open_price REAL,last_close REAL)''')for stock in stocks_data:# Parse stock datastock_info = {}attributes = stock.split(',')for attribute in attributes:key_value = attribute.split(':')key = key_value[0].strip('"')value = key_value[1].strip('"')stock_info[key] = value# Extract relevant stock informationstock_code = stock_info.get('f12', 'N/A')stock_name = stock_info.get('f14', 'N/A')stock_price = float(stock_info.get('f2', 0.0))price_change = float(stock_info.get('f4', 0.0))price_change_percent = float(stock_info.get('f3', 0.0))volume = int(stock_info.get('f5', 0))turnover = float(stock_info.get('f6', 0.0))amplitude = float(stock_info.get('f7', 0.0))highest = float(stock_info.get('f15', 0.0))lowest = float(stock_info.get('f16', 0.0))open_price = float(stock_info.get('f17', 0.0))last_close = float(stock_info.get('f18', 0.0))# Insert stock data into the databasedb_cursor.execute("INSERT INTO stock_info (stock_code, stock_name, stock_price, price_change, price_change_percent, volume, turnover, amplitude, highest, lowest, open_price, last_close) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",(stock_code, stock_name, stock_price, price_change, price_change_percent, volume, turnover, amplitude,highest, lowest, open_price, last_close))database_connection.commit()# Query all stock datadb_cursor.execute("SELECT * FROM stock_info")all_stocks = db_cursor.fetchall()# Get column names for displaying datacolumn_names = [description[0] for description in db_cursor.description]print("\t".join(column_names))# Display each stock's informationfor stock in all_stocks:print("\t".join(map(str, stock)))# Close the database connectiondatabase_connection.close()# Execute the function with specified parameters
    page_number = 1
    process_stock_data("fid=f3&fs=m:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23,m:0+t:81+s:2048", page_number)

    结果如下:

2.心得体会

​ 目前代码是一次性抓取数据,可以考虑添加功能以定期更新数据或只插入新数据,而不是每次都插入,且如果数据量增加,插入数据库时可以考虑使用批量插入的方式提高性能。

作业三

1.实验内容

  • 要求:爬取中国大学2021主榜(https://www.shanghairanking.cn/rankings/bcur/2021)所有院校信息,并存储在数据库中,同时将浏览器F12调试分析的过程录制Gif加入至博客中。
  • 技巧:分析该网站的发包情况,分析获取数据的api

代码如下:

import requests
from bs4 import BeautifulSoup
import sqlite3# 目标网页URL
url = 'https://www.shanghairanking.cn/rankings/bcur/2021'  # 请替换为实际的URL# 使用 requests 获取网页内容
response = requests.get(url)
content = response.content# 解析网页内容
soup = BeautifulSoup(content, 'html.parser')# 找到包含大学排名信息的表格
ranking_table = soup.find('table', class_='rk-table')# 创建或连接到SQLite数据库
database = sqlite3.connect('schools_rank.db')
db_cursor = database.cursor()# 创建数据表,如果表不存在
db_cursor.execute('''CREATE TABLE IF NOT EXISTS university_ranking(rank TEXT, school_name TEXT, province_city TEXT, school_type TEXT, total_score TEXT)''')# 遍历表格中的每一行,获取数据
for entry in ranking_table.find_all('tr')[1:]:  # 跳过表头columns = entry.find_all('td')rank_value = columns[0].get_text(strip=True)university_name = columns[1].get_text(strip=True)location = columns[2].get_text(strip=True)type_of_school = columns[3].get_text(strip=True)score = columns[4].get_text(strip=True)# 插入数据到数据库中db_cursor.execute('''INSERT INTO university_ranking (rank, school_name, province_city, school_type, total_score)VALUES (?, ?, ?, ?, ?)''', (rank_value, university_name, location, type_of_school, score))# 提交数据到数据库
database.commit()# 查询并打印数据库中的所有记录
db_cursor.execute("SELECT * FROM university_ranking")
records = db_cursor.fetchall()
for record in records:# 清除记录中的换行符cleaned_record = [field.replace('\n', '') for field in record]# 打印处理后的记录print(tuple(cleaned_record))# 关闭数据库连接
database.close()print("大学排名数据已成功保存至数据库")

结果如下:

F12调试过程GIF如下:

2.心得体会

​ 当前代码没有包括任何异常处理逻辑,导致我在debug时花费较多时间。例如在数据库操作或网页请求时,如果发生错误,程序将直接崩溃。下次将在关键部分加入 try-except 块,以便能够捕获并处理潜在的异常。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/72875.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

Hadoop3.X高可用环境搭建

目录一.基础环境准备1.角色分配2.准备软件源3.部署JDK环境4.配置host文件解析5.配置ssh免密登录6.编写data_rsync.sh同步脚本二.安装zookeeper环境1.zookeeper集群的节点数量选择2.下载zookeeper软件3.解压软件包4.配置环境变量5.准备配置文件6.同步数据7.生成myid文件8.启动zo…

安装虚拟机

每一步都很清晰,包括配置网络,一些基本的使用安装虚拟机CentOS 创建时间:2024-1016 地址: 通过网盘分享的文件:镜像文件 链接: https://pan.baidu.com/s/1Up2IrB_hzXPc0omfhrkl9Q?pwd=b82q 提取码: b82q --来自百度网盘超级会员v6的分享 1.准备好镜像2.在vmware 创建虚拟…

Codeforces Round 893 (Div. 2)题解记录

题目链接:https://codeforces.com/contest/1858 A. Buttons从自身角度出发,我想赢就得保证我的按钮尽可能多所以,大家都优先按\(c\),然后分析先后顺序即可#include<iostream>#include<string.h>#include<map>#include<vector>#include<set>#i…

细胞大小,真核细胞直径平均: 3~30μm;

细胞大小 1毫米=1000微米, 1微米=1000纳米, 1毫米=1000000纳米。 单位有毫米(mm)、微米(μm)、纳米(nm) 。细胞的大小是细胞的重要特征,各类细胞的大小是有一定规律的。一般来说,原核细胞直径平均: 1~10μm; 真核细胞直径平均: 3~30μm; 某些不同来源的细胞大小变…

Spring基础

一、什么是 Spring 框架?Spring是一款开源的轻量级Java开发框架,旨在提高开发人员的开发效率以及系统的可维护性。  我们一般说 Spring 框架指的都是 Spring Framework,它是很多模块的集合,使用这些模块可以很方便地协助我们进行开发,比如说 Spring 支持 IoC(Inversion…

《使用Gin框架构建分布式应用》阅读笔记:p77-p87

《用Gin框架构建分布式应用》学习第5天,p77-p87总结,总计11页。 一、技术总结 1.Go知识点 (1)context 2.on-premises software p80, A container is like a separate OS, but not virtualized; it only contains the dependencies needed for that one application, which ma…

排序算法 - 快速排序

排序算法 - 快速排序概要快速排序(Quicksort)是对冒泡排序算法的一种改进。快速排序是一种基于分而治之的排序算法。它的基本思想是: 选择一个基准数,通过一趟排序将要排序的数据分割成独立的两部分;其中一部分的所有数据都比另外一部分的所有数据都要小。然后,再按此方法…

学习groovy基础

简介 Groovy 是一种 JVM 语言,它可以编译与 Java 相同的字节码,然后将字节码文件交给 JVM 去执行,并且可以与 Java 无缝地互操作,Groovy 可以透明地与 Java 库和代码交互,可以使用 Java 所有的库。 Groovy 也可以直接将源文件解释执行 它还极大地清理了 Java 中许多冗长的…