俗话说,唐诗三百首 Python代码
时间: 2023-11-19 20:17:27 浏览: 111
以下是一个使用 Python 实现打印唐诗三百首的简单例子:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.gushiwen.org/gushi/tangshi.aspx'
# 获取所有唐诗的链接
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
poem_links = soup.select('.typecont a')
# 遍历所有唐诗链接并打印诗歌内容
for link in poem_links:
poem_url = 'https://www.gushiwen.org' + link['href']
response = requests.get(poem_url)
soup = BeautifulSoup(response.text, 'html.parser')
poem = soup.select('.contson')[0].text.strip()
print(poem)
```
注意:这个例子只是一个简单的爬虫示例,如果要用于其他用途,需要注意版权问题。
阅读全文