python獲取藏頭詩 3 創作詩詞

2021-10-12 17:54:16 字數 1754 閱讀 5036

# coding=utf-8

import re

import linecache

import random

import

time

hs = 649620 # 總數

name = input(

"輸入要創作的五言頂頭詩:"

)len_n = input(

"藏於第幾個字(請輸入1-5,也可嘗試7-11):"

)len_n = int(len_n)

len_n = len_n-1

name_sz =

# 用於存放名字

for i2 in name:

print(name_sz)

name_long = len(name_sz)

print(name_long)

for num in range(name_long):

linecache.clearcache(

) i = name_sz[num]

print(i+":正在查詢......."

) sc_lien = 1 # 讀取行數

n_err = 0 # 找不到次數

for num2 in range(hs):

str_sc = linecache.getline(

"sc2.txt", sc_lien)

try:

a1 = re.search(i, str_sc).span(

) with open(

"sc/"+i+".txt", "a", encoding=

"utf-8"

) as f: #使用utf-8編碼防止錯誤寫入

if a1[0]

== len_n:

# print(str(sc_lien) + str_sc)

f.write(str_sc)

else:

n_err += 1

except:

n_err += 1

sc_lien += 1

# print(i+"一共找到"+str(hs-n_err))

linecache.clearcache(

)print(

"正在創作"

)time.sleep(3)

for end_tim in range(3):

print(

"第"+str(end_tim+1)+"首"

)for mun_1 in range(name_long):

try:

c_yi = name_sz[mun_1]

filename =

"sc/"+c_yi+".txt"

myfile = open(filename, encoding=

"utf-8"

) lines = len(myfile.readlines(

)) ran = random.randint(1, lines)-1

myfile.close(

) linecache.clearcache(

) cy = linecache.getline(filename, ran)

print(cy)

except:

print(

"翻遍天下名書也找不到這個字》"

)time.sleep(300)

Python藏頭詩創作教程

這裡我們用urllib庫來進行爬蟲,然後用re和beautifulsoup來進行解析 爬取的 是 然後將爬取的古詩儲存在c 程式設計 python poem.txt中 import codecs from urllib.request import urlopen import re from bs...

python3獲取請求cookie

github鏈結 使用python庫直接獲取cookie defget cookie url param url 請求連線應保證伺服器有set cookie寫入操作 return 請求負載 data 請求頭 headers try 宣告乙個cookiejar物件例項來儲存cookie cookie ...

Python3 獲取ajax 返回內容

使用工具 瀏覽器chrome 或者firefox python3 什麼樣的頁面是使用了ajax技術?留給你回答.其中method有post和get兩種方法。區別就是get,那麼request url就是這個鏈結的返回值,使用json格式輸出 如果是post,則request url返回一般是空,aj...