0基礎0元學習Python-第三天

李雷的學習來到了第三天。居然能堅持到第三天,不容易呀~

 

複習第二天的內容

學會了使用Python讀取文件,獲得文件裏的數據。

學會了通過Python使用正則表達式匹配字符串。正則表達式使得查詢變得強大而且簡單。使用的方式如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import re
s = ''
# 打開文件,把數據存入s變量,使用with打開,可以自動處理釋放資源
with open('level3.txt') as file_:
  s = ''.join(line.rstrip() for line in file_ )

# 通過正則表達式,獲得匹配好的數據,re.M表示多行匹配,這裏其實不是必須,因爲上一步已經把字符串處理成了一行
searchAll = re.finditer( r'[a-z][A-Z]{3}([a-z])[A-Z]{3}[a-z]', s, re.M)

# 這個變量用來存放所有的匹配出來的小寫字母
letter = ''

# 因爲使用了re.finditer函數,需要利用循環來讀取匹配到的結果,每一次循環獲得匹配到的一個結果
for match in searchAll:
   print(match.group())
   print(match.group(1))
   letter = letter + match.group(1) # 因爲在模式字符串中寫了(),所以使用group(1)能獲得括號裏的內容

print letter

 

第4題

第二天李雷來到了第4題,是一個循環題目

頁面裏沒有任何提示,但是圖片可以點擊,點擊後的頁面是:http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345

頁面顯示and the next nothing is 44827。然後訪問http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=44827

頁面顯示and the next nothing is 45439,接着訪問http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=45439

頁面顯示Your hands are getting tired and the next nothing is 94485,接着訪問http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=94485

頁面顯示and the next nothing is 72198,接着訪問http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=72198

頁面顯示and the next nothing is 80992,接着訪問http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=80992

......

訪問一個頁面得到下一個頁面的地址。這是一個編程能發揮作用的絕佳機會,就是重複,重複的訪問並獲得下一個鏈接。

程序能不知疲倦的重複執行一個操作,沒有抱怨。李雷先學習瞭如何使用Python訪問網頁,然後思考如何寫出讓程序循環的訪問這個頁面,或者下次頁面的參數再訪問。根據一些經驗,這裏李雷覺得可以使用while循環,直到找到頁面返回的內容不是and the next nothing is格式的,再停止。

爲了識別這個格式,需要用到第二天學到的正則表達式。經過2個多小時的反覆調試和實驗,最後代碼如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import re
import urllib

# 用next變量存開始頁面的參數
next = '82972'

# 循環開始,只要返回的內容存在就一直執行
while (next):
  # 使用頁面參數拼接url打開一個網頁
  page = urllib.urlopen('http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=' + next)
  # 讀取數據
  data = page.read()
  # 判斷數據是否符合特定格式
  matchObj = re.match(r'and the next nothing is (\d+)', data)
  # 如果滿足特定格式,更新next變量爲最新的參數
  if matchObj:
    next = matchObj.group(1)
  else:
    # 如果不滿足格式,請求next,這個時候while循環會停止
    next = ''
  # 輸出一下頁面返回的結果
  print('Data:', data.decode('utf-8'))

代碼運行截圖:

程序輸出的最後一行是peak.html。哈哈哈,來到了下一題。

這個代碼有很多缺陷,其中最大的一個是沒有考慮到打開網頁的時候會超時。

 

第5題

打開http://www.pythonchallenge.com/pc/def/peak.html,李雷看到了一幅圖

盯了半天,不知所云,還是看源代碼吧。

<html>
<head>
  <title>peak hell</title>
  <link rel="stylesheet" type="text/css" href="../style.css">
</head>
<body>
<center>
<img src="peakhell.jpg"/>
<br><font color="#c0c0ff">
pronounce it
<br>
<peakhell src="banner.p"/>
</body>
</html>

<!-- peak hell sounds familiar ? -->

不用多說,李雷趕緊在搜索引擎裏搜索“Python .p文件”,看看能發現什麼。

找到了相關的文章:

https://blog.csdn.net/cybeyond_xuan/article/details/82985066

https://blog.csdn.net/Eve_if/article/details/84590455

發現.p文件是使用cPickle庫序列化後的文件,可以使用cPickle庫反序列化會序列化之後的文件, 李雷把.p文件下載回來,然後參考網上方法反序列化試一試。實際測試,發現本地沒有cPickle庫,但是有pickle庫可以使用,是一樣的。

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import pickle
import re

dumps_content = ''
# 打開文件,把數據保存在dumps_content裏
with open('banner.p') as file_:
  # 讀取整個文件,不需要處理結尾空白
  dumps_content = file_.read()

# load一下整個文件,反序列化
loads_content = pickle.loads(dumps_content)
print(loads_content)

輸出了一個二位數組,如圖:

李雷仔細看了看裏面的元素,彷彿發現了什麼。第一個元素的數組裏包含一個元組,元組第二個元素是95,第二個元素的數組裏有5個元組,把這些元組的第二個元素加起來也是95。這貌似是表示一行有95個位置,每個元組表示這一行對應的列應該展示什麼。如果有沒有猜錯的花,應該會輸出一個圖形來。李雷趕緊試一試通過兩層循環,根據這個元組規則打印出來看看。

經過一個小時的嘗試和練習,最後李雷寫出來代碼:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
from __future__ import print_function
import pickle
import re

dumps_content = ''
# 打開文件,把數據保存在dumps_content裏
with open('banner.p') as file_:
  # 讀取整個文件,不需要處理結尾空白
  dumps_content = file_.read()

# load一下整個文件,反序列化
loads_content = pickle.loads(dumps_content)
# 循環每一行
for row in loads_content:
  # 循環每一行的多元素
  for item in row:
    # 對每個元素,根據多個,循環打印出字符
    for r in range(item[1]):
      # 輸出字符不換行
      print(item[0], end='')
  # 每一行循環完以後換行
  print('')

這個代碼運行以後會輸出:

cool。李雷順(艱難)利(努力)  來到了第6題。http://www.pythonchallenge.com/pc/def/channel.html

 

第6題

http://www.pythonchallenge.com/pc/def/channel.html,有點色情,感覺是褲子拉鍊開了。

還有作者的捐助按鈕,難道想表達的是捐錢可以繼續往下拖?李雷看了一下源代碼。


<html> <!-- <-- zip -->
<head>
  <title>now there are pairs</title>
  <link rel="stylesheet" type="text/css" href="../style.css">
</head>
<body>
<center>
<img src="channel.jpg">
<br/>
<!-- The following has nothing to do with the riddle itself. I just
thought it would be the right point to offer you to donate to the
Python Challenge project. Any amount will be greatly appreciated.

-thesamet
-->

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="cmd" value="_xclick">
    <input type="hidden" name="business" value="[email protected]">
    <input type="hidden" name="item_name" value="Python Challenge donations">
    <input type="hidden" name="no_note" value="1">
    <input type="hidden" name="currency_code" value="USD">
    <input type="hidden" name="tax" value="0">
    <input type="hidden" name="bn" value="PP-DonationsBF">
    <input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but04.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
    <img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>

</body>
</html>

作者勸李雷充值,但是李雷並沒有paypal,上個alipay,李雷或許考慮充1塊錢感謝作者的題目。李雷看出來這是一個謎語題,只能靠傳家寶了,猜猜猜。

根據第一行的提示,輸入一個channle.zip試一試,沒有。然後輸入zip.html。頁面提示:

yes, find the zip.

確認這個思路是正確的。

再試一次channel.zip。文件下載成功,第一次沒有成功,應該是在源代碼頁面輸入zip文件會觸發下載,在源代碼頁面無效了。

這個壓縮包裏有很多文件。裏面一個 readme.txt文件,裏面是這樣寫着:

welcome to my zipped list.

hint1: start from 90052
hint2: answer is inside the zip

打開90052看看,裏面寫着:

Next nothing is 94191

看來和第5題一樣的,只不過是把網頁換成了文件。換湯不換藥💊呀~

 

總結

李雷已經基本能讀取文件,使用這則表達式。

並且實踐瞭如何訪問網頁,反序列化字符串。

處理元組和數組。

 

第四天加油~

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章