日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python命令行运行模式_[Python] 命令行模式阅读博客园的博文

發布時間:2023/12/1 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python命令行运行模式_[Python] 命令行模式阅读博客园的博文 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1 #-*- coding:UTF-8 -*-

2 importrequests3 from lxml importetree4 importsys5 importio6 importos7

8

9 sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='gb18030')10

11

12 classCnBlogs:13 """"14 Auth:reader15 發表地址:https://www.cnblogs.com/reader/p/11487398.html16 作者地址:https://www.cnblogs.com/reader17 """

18 def __init__(self):19 self.headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36'}20

21 self.target_domain = "https://www.cnblogs.com"

22 self.page = 1

23 self.lists ={}24

25 defclearscreen(self):26 """根據系統,清屏操作"""

27 #window下的清屏方式

28 os.system("cls")29

30 defset_target_url(self, page):31 if page == 1:32 self.target_url =self.target_domain33 else:34 self.target_url = 'https://www.cnblogs.com/sitehome/p/'+str(page)35

36 defdownload(self, page):37 """下載html頁面內容"""

38 self.set_target_url(page)39 response = requests.get(self.target_url, headers=self.headers)40 if response.status_code == 200:41 returnresponse.content42 else:43 print("download fail")44 return ""

45

46 defisascii(self, ch):47 return ch <= u'\u007f'

48

49 defformatByWidth(self, text, width):50 """格式化字符串長度"""

51 count =052 for u intext:53 if notself.isascii(u):54 count += 1

55 return text + " " * (width - count -len(text))56

57 defparse(self, content):58 """解析HTML內容"""

59 html =etree.HTML(content)60 lists = html.xpath('//div[@id="post_list"]//div[@class="post_item_body"]')61

62 delhtml63 k = 1

64 print('+', '--' * 50, '+')65 print('|', str("當前頁碼:"+str(self.page)).ljust(95), '|')66 print('+', '--' * 50, '+')67 for li inlists:68 title = str(li.xpath('h3/a/text()')[0])69 link = li.xpath('h3/a/@href')[0]70 desc = li.xpath('p')[0].xpath('string(.)')71

72 self.lists[k] ={73 'title': title,74 'desc': desc.strip(),75 'link': link76 }77

78 print('|', k, self.formatByWidth(title, 100-1-len(str(k))), '|')79 k += 1

80 dellists81 print('+', '--' * 50, '+')82

83 defdescopt(self, k):84 """讀取詳情"""

85 k =int(k)86 if k not inself.lists.keys():87 return

88 self.clearscreen()89 print('+', '--' * 50, '+')90 print('|', self.formatByWidth(self.lists[k]['title'], 100), '|')91 print('+', '--' * 50, '+')92 print('|', self.formatByWidth(self.lists[k]['link'], 100), '|')93 print('+', '--' * 50, '+')94

95 print('|', self.formatByWidth(self.lists[k]['desc'], 100), '|')96

97 print('+', '--' * 50, '+')98 input("輸入任意鍵返回...\r\n")99

100 defreadopt(self):101 """開始閱讀操作"""

102 whileTrue:103 self.clearscreen()104 print("\r\n")105 html = self.download(page=self.page)106 self.parse(html)107

108 print("[N]:下一頁,[B]:上一頁,[H]:首頁,[D {num}]:簡述, [Q]:返回")109

110 cmd = input("請輸入操作編號[N、B、H、D、Q]:")111

112 if cmd == 'Q' or cmd == 'q': #返回

113 break

114 elif cmd == 'N' or cmd == 'n': #下一頁

115 self.page += 1

116 elif cmd == 'B' or cmd == 'b': #上一頁

117 self.page -= 1

118 if self.page <=0:119 self.page = 1

120 elif cmd == 'H' or cmd == 'h': #首頁

121 self.page = 1

122 else:123 cmd = cmd.split(' ')124

125 if len(cmd) != 2:126 continue

127 #讀取簡述

128 if cmd[0] == 'D' or cmd[0] == 'd':129 self.descopt(cmd[1])130

131 defaboutopt(self):132 self.clearscreen()133 print("博客園地址: https://www.cnblogs.com/reader\r\n")134 input("輸入任意鍵返回...\r\n")135

136 defstart(self):137 self.clearscreen()138 whileTrue:139 print('+', '--'*50, '+')140 print('|', "歡迎使用博客園閱讀器(reader 開發)".center(88), '|')141 print('+', '--' * 50, '+')142 print('|', "[1]:開始閱讀".center(95), '|')143 print('|', "[2]:關于作者".center(95), '|')144 print('|', "[Q]:退出軟件".center(95), '|')145 print('+', '--' * 50, '+')146

147 cmd = input("請輸入操作編號[1、2、Q]:")148 if cmd == '1':149 self.readopt()150 elif cmd == '2':151 self.aboutopt()152 elif cmd == 'Q' or cmd == 'q':153 break

154

155 os.system("cls")156

157 print("已退出,歡迎使用!")158

159

160 if __name__ == "__main__":161 obj =CnBlogs()162 obj.start()

總結

以上是生活随笔為你收集整理的python命令行运行模式_[Python] 命令行模式阅读博客园的博文的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。