201_搜尋字詞

題目說明:

請開啟PYD02.py檔案,依下列題意進行作答,使輸出值符合題意要求。作答完成請另存新檔為PYA02.py再進行評分。

設計說明:

請撰寫一程式,爬取http://tqc.codejudger.com:3000/target/5201.html,程式須回傳下列資訊:
讓使用者輸入欲搜尋的字詞,再輸出字詞的搜尋結果及字詞出現的次數。

輸入輸出:

輸入說明

爬取網頁
搜尋的字詞

輸出說明

字詞的搜尋結果
字詞出現的次數

輸入輸出範例:

範例輸入_1

範例輸出_1

1637821423441

待編修檔案:

# 載入模組
import ___
import ___

url = '___'

# 使用 GET 請求
htmlfile = requests.___(___)
# 驗證HTTP Status Code
if htmlfile.status_code == ___:
    # 欲搜尋的字串
    ___ = input("請輸入欲搜尋的字串 : ")
    ___ = re.findall(___, htmlfile.text)
    if ___ in htmlfile.text:
        print(___, "搜尋成功")
        print(___, "出現 %d 次" % len(___))
    else:
        print(___, "搜尋失敗")
        print(___, "出現 0 次")
else:
    print("網頁下載失敗")

 

程式碼_1:

# 載入模組
import requests
import re

url = 'http://tqc.codejudger.com:3000/target/5201.html'

# 使用 GET 請求
htmlfile = requests.get(url)
# 驗證HTTP Status Code
if htmlfile.status_code == 200:
    # 欲搜尋的字串
    key = input("請輸入欲搜尋的字串 : ")
    datas = re.findall(key, htmlfile.text)
    if key in htmlfile.text:
        print(key, "搜尋成功")
        print(key, "出現 %d 次" % len(datas))
    else:
        print(key, "搜尋失敗")
        print(key, "出現 0 次")
else:
    print("網頁下載失敗")


 搭 配 學 習 

  • Python 3.x 網頁資料擷取與分析特訓教材風_碁峰

    博客來      誠品

  • TQC+ Python 3.x網頁資料擷取與分析特訓教材_全華

    博客來      誠品

 

arrow
arrow

    DING BANG 發表在 痞客邦 留言(0) 人氣()