PythonでHTMLファイルから文字を数える
pythonchallenge.comでPythonチャレンジのレベル2を完了したばかりで、Pythonを学習している最中なので、私と私が犯したかもしれないばかげた間違いに耐えてください。
私は自分のコードで何ができたのかについてのフィードバックを探しています。具体的には2つの領域:
- HTMLファイルのコメントセクションをより簡単に識別するにはどうすればよいですか?コメントの終わり(または技術的には最初ですが、最後から数えています)を見つけて、認識して予想できたいくつかの追加の文字(追加の「->」および「-」)。カウントする新しい文字列にコメントを入れることができるように、このコメントを見つけたほうがよい条件は何ですか?
これは私が書いたものです:
from collections import Counter
import requests
page = requests.get('http://www.pythonchallenge.com/pc/def/ocr.html')
pagetext = ""
pagetext = (page.text)
#find out what number we are going back to
i = 1
x = 4
testchar = ""
testcharstring = ""
while x == 4:
testcharstring = pagetext[-i:]
testchar = testcharstring[0]
if testchar == "-":
testcharstring = pagetext[-(i+1)]
testchar = testcharstring[0]
if testchar == "-":
testcharstring = pagetext[-(i+2)]
testchar = testcharstring[0]
if testchar == "!":
testcharstring = pagetext[-(i+3)]
testchar = testcharstring[0]
if testchar == "<":
x = 3
else:
i += 1
x = 4
else:
i += 1
x = 4
else:
i += 1
print(i)
newstring = pagetext[-i:]
charcount = Counter(newstring)
print(charcount)
そしてこれはソースHTMLです:
<html>
<head>
<title>ocr</title>
<link rel="stylesheet" type="text/css" href="../style.css">
</head>
<body>
<center><img src="ocr.jpg">
<br><font color="#c03000">
recognize the characters. maybe they are in the book, <br>but MAYBE they
are in the page source.</center>
<br>
<br>
<br>
<font size="-1" color="gold">
General tips:
<li>Use the hints. They are helpful, most of the times.</li>
<li>Investigate the data given to you.</li>
<li>Avoid looking for spoilers.</li>
<br>
Forums: <a href="http://www.pythonchallenge.com/forums"/>Python Challenge Forums</a>,
read before you post.
<br>
IRC: irc.freenode.net #pythonchallenge
<br><br>
To see the solutions to the previous level, replace pc with pcc, i.e. go
to: http://www.pythonchallenge.com/pcc/def/ocr.html
</body>
</html>
<!--
find rare characters in the mess below:
-->
<!--
数千の文字が続き、コメントは「->」で終わります
回答
コメントするのに十分な評判がないので、答えの中でこれを言わなければなりません。使用するのは不格好に見えます
while x == 4:
そしてします
x = 3
ループから抜け出したいときはいつでも。やったほうがいいようです
while True:
ループから抜け出したいときは
break
乾杯!
冗長コード
pagetext = ""
pagetext = (page.text)
最初の行は、空の文字列をに割り当てますpagetext。2行目は、すでに入っている内容を無視pagetextし、変数に別の値を割り当てます。
なぜ最初のステートメントを気にするのですか?それは単にコードを長くし、遅くし、理解しにくくします。
なぜ(...)周りを気にするのpage.textですか?また、それらは何の目的も果たしていません。
変数名
のような変数iは両刃の剣です。これをループインデックスとして使用しており、ループの終了後に見つかった場所を参照するために使用しています。しかしi、それ自体はあまり意味がありません。posnより明確かもしれません。last_comment_posn非常に冗長ですが、はるかに明確になります。
PEP-8では、変数名の単語を区切るためにアンダースコアを使用することをお勧めします。つまり、char_countnotcharcountなどを使用します。
文字列の検索
Python文字列には、より大きな文字列で部分文字列を検索するための組み込み関数があります。たとえば、ページテキストでstr.find最初に出現するものをすばやく見つけることができ<!--ます。
i = pagetext.find("<!--")
しかし、あなたは最初のものを探していません。あなたは最後のものを探しています。Pythonは、逆検索関数を使用して、再びカバーしましたstr.rfind。
i = pagetext.rfind("<!--")
しかし、これはまだ最後の発生のインデックスを見つけます。コメントマーカーの後の文字が必要なので、さらに4文字スキップする必要があります。
if i >= 0:
newstring = pagetext[i+4:]
改善されたコード
import requests
from collections import Counter
page = requests.get('http://www.pythonchallenge.com/pc/def/ocr.html')
page.raise_for_status() # Crash if the request didn't succeed
page_text = page.text
posn = page_text.rfind("<!--")
print(posn)
if posn >= 0:
comment_text = page_text[posn+4:] # Fix! This is to end of string, not end of comment!
char_count = Counter(comment_text)
print(char_count)