使用python 下載全國曆代古詩

zz891422822發表於2020-12-22

使用python 下載全國曆代古詩

開啟CMD 安裝以下程式
pip install requests
pip install lxml在這裡插入圖片描述
以下是全部程式碼

import requests
from lxml import etree
import re

headers = {
    'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'
}

si_ci = 17234
while si_ci <= 298560:
    url = "http://www.zhongguoshici.com/shici/details/part/"+str(si_ci)+"?type=audio"
    response = requests.get(url, headers=headers)
    req = re.findall(r'《(.*?)》', response.text)
    try:
        print(str(si_ci)+"."+req[0])
        html = etree.HTML(response.text)
        html_data = html.xpath('//p')
        with open("01.txt", "a") as f:
            for i in html_data:
                data_sg = i.text
                f.write(data_sg+"(出自-"+req[0]+")"+"\n")
        si_ci += 1
    except:
        print(str(si_ci)+"異常")
        si_ci += 1

在這裡插入圖片描述

相關文章