Gitee地址鏈接:https://gitee.com/Bluemingiu/project/tree/master/2
作業(yè)①
要求:在中國氣象網(wǎng)(http://www.weather.com.cn)給定城市集的7日天氣預(yù)報(bào),并保存在數(shù)據(jù)庫。
(1) 實(shí)驗(yàn)過程
核心代碼:
使用 requests 庫發(fā)送 HTTP 請(qǐng)求,并用 BeautifulSoup 解析 HTML 內(nèi)容,以提取天氣預(yù)報(bào)信息。
for city, url in cities.items():
response = requests.get(url)
print(f"正在爬取: {city} ({url}) - 狀態(tài)碼: {response.status_code}") # 打印狀態(tài)碼
if response.status_code == 200:
response.encoding = response.apparent_encoding # 設(shè)置正確的編碼
soup = BeautifulSoup(response.text, 'html.parser')
forecasts = soup.find_all('ul', class_='t clearfix')
數(shù)據(jù)插入數(shù)據(jù)庫:
INSERT INTO weather (city, date, weather, temperature) VALUES (?, ?, ?, ?)
''', (city, date, weather, temperature))
輸出結(jié)果:

數(shù)據(jù)庫查詢結(jié)果:

(2) 心得體會(huì)
模塊化和結(jié)構(gòu)化的代碼讓功能分明,使用 requests 和 BeautifulSoup 能夠高效處理網(wǎng)絡(luò)請(qǐng)求和 HTML 解析。此外,錯(cuò)誤處理的重要性不容忽視,網(wǎng)絡(luò)請(qǐng)求可能失敗且網(wǎng)站結(jié)構(gòu)會(huì)變化,因此要靈活應(yīng)對(duì)。
作業(yè)②
要求:用requests和BeautifulSoup庫方法定向爬取股票相關(guān)信息,并存儲(chǔ)在數(shù)據(jù)庫中。
(1) 實(shí)驗(yàn)過程
選取網(wǎng)站:東方財(cái)富網(wǎng):https://www.eastmoney.com/
核心代碼:
數(shù)據(jù)抓取,從指定的 API 獲取股票數(shù)據(jù)并解析 JSON
def fetch_stock_data(page):
url = f"http://64.push2.eastmoney.com/api/qt/clist/get?cb=jQuery&pn={page}&pz=20&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&wbp2u=|0|0|0|web&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&fields=f2,f3,f4,f5,f6,f7,f12,f14&_=1696660618067"
response = requests.get(url)
json_data = response.text[response.text.find('(')+1:response.text.rfind(')')]
data = json.loads(json_data)
return data.get('data', {}).get('diff', [])
數(shù)據(jù)存儲(chǔ),將抓取到的股票數(shù)據(jù)存儲(chǔ)到 SQLite 數(shù)據(jù)庫中
def store_data(stocks):
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
for stock in stocks:
if isinstance(stock, dict): # 確保 stock 是字典
cursor.execute('''
INSERT INTO stocks (name, code, price, change, percent_change, volume) VALUES (?, ?, ?, ?, ?, ?)
''', (
stock.get('f12'), # 股票名稱
stock.get('f14'), # 股票代碼
stock.get('f2'), # 當(dāng)前價(jià)格
stock.get('f3'), # 漲跌額
stock.get('f4'), # 漲跌幅
stock.get('f6') # 成交量
))
conn.commit()
conn.close()
輸出結(jié)果:



數(shù)據(jù)庫查詢結(jié)果:

(2)心得體會(huì)
抓取數(shù)據(jù)時(shí)需注意 API 的請(qǐng)求限制和數(shù)據(jù)格式,這要求在編程中保持靈活性和對(duì)錯(cuò)誤的處理能力。其次,將數(shù)據(jù)有效地存儲(chǔ)到數(shù)據(jù)庫中不僅要考慮性能,還要確保數(shù)據(jù)的完整性和準(zhǔn)確性。
作業(yè)③
要求:爬取中國大學(xué)2021主榜(https://www.shanghairanking.cn/rankings/bcur/2021)所有院校信息,并存儲(chǔ)在數(shù)據(jù)庫中,同時(shí)將瀏覽器F12調(diào)試分析的過程錄制Gif加入至博客中。錄制Gif加入至博客中。
(1) 實(shí)驗(yàn)過程
核心代碼:
將數(shù)據(jù)儲(chǔ)存到數(shù)據(jù)庫中
# 存儲(chǔ)數(shù)據(jù)到SQLite數(shù)據(jù)庫
def store_data(df):
conn = sqlite3.connect('university_rankings.db')
df.to_sql('rankings', conn, if_exists='replace', index=False)
conn.close()
輸出結(jié)果:

數(shù)據(jù)庫查詢:

F12調(diào)試分析的過程:

(2) 心得體會(huì)
完成這個(gè)任務(wù)讓我深刻體會(huì)到數(shù)據(jù)抓取的重要性,它是高效收集信息的關(guān)鍵。同時(shí),正則表達(dá)式的靈活性使得提取特定數(shù)據(jù)變得可能,盡管需要仔細(xì)設(shè)計(jì)以確保準(zhǔn)確性。使用 SQLite 進(jìn)行數(shù)據(jù)存儲(chǔ)簡單有效,適合小型項(xiàng)目,理解數(shù)據(jù)的持久化和結(jié)構(gòu)化存儲(chǔ)對(duì)后續(xù)分析至關(guān)重要。
浙公網(wǎng)安備 33010602011771號(hào)