融合门户
import requests
from bs4 import BeautifulSoup
def fetch_news(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
headlines = []
for article in soup.find_all('article'):
headline = article.find('h2').text.strip()
headlines.append(headline)
return headlines
url = "https://example-news-site.com"
news_headlines = fetch_news(url)
print("抓取到的新闻标题:", news_headlines)
这段代码会从指定URL抓取新闻标题。

from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
url = "https://example-news-site.com"
headlines = fetch_news(url)
return render_template('index.html', headlines=headlines)
if __name__ == '__main__':
app.run(debug=True)
你需要确保有一个名为`templates/index.html`的文件来显示这些新闻。