九行代码爬取上下五千年描写春天的诗名

效果展示:
九行代码爬取上下五千年描写春天的诗名_第1张图片
代码如下:

import requests
from bs4 import BeautifulSoup

url = "https://www.gushiwen.org/gushi/chuntian.aspx"
response = requests.get(url)
content = response.text

soup = BeautifulSoup(content, "html.parser")

titles = soup.select(".typecont span a")

with open("chuntian_titles.txt", "w", encoding="utf-8") as file:
    for title in titles:
        title_text = title.text.strip()
        file.write(title_text + "\n")


你可能感兴趣的:(数据分析,python,windows,运维,数据库)