連結金融新聞和資訊:
結金融新聞和相關資訊,以提供使用者最新的金融資訊和幫助投資決策,您可以考慮下列方法:
以下是使用Python的示例程式碼,用於爬取PTT股票板和Reddit r/Stocks的文章:
首先,我們將使用requests
庫來向PTT和Reddit伺服器發出請求並獲取頁面的HTML內容。然後,使用BeautifulSoup
庫來解析HTML並提取我們需要的資訊。
import requests
from bs4 import BeautifulSoup
# 爬取PTT股票板文章
def crawl_ptt_stocks():
url = "https://www.ptt.cc/bbs/Stock/index.html"
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
articles = soup.find_all("div", class_="title")
for article in articles:
title = article.a.text.strip()
print(title)
# 爬取Reddit r/Stocks文章
def crawl_reddit_stocks():
url = "https://www.reddit.com/r/Stocks"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, "html.parser")
articles = soup.find_all("h3", class_="_eYtD2XCVieq6emjKBH3m")
for article in articles:
title = article.text.strip()
print(title)
# 執行爬蟲
crawl_ptt_stocks()
crawl_reddit_stocks()