を解析し、私の基本コードです:は、ここでは、ウェブサイト
import urllib
import webbrowser
from bs4 import BeautifulSoup
import requests
address = 'https://google.com/search?q='
# Default Google search address start
file = open("OCR.txt", "rt")
# Open text document that contains the question
word = file.read()
file.close()
myList = [item for item in word.split('\n')]
newString = ' '.join(myList)
# The question is on multiple lines so this joins them together with proper spacing
qstr = urllib.parse.quote_plus(newString)
# Encode the string
newWord = address + qstr
# Combine the base and the encoded query
response = requests.get(newWord)
#with open('output.html', 'wb') as f:
# f.write(response.content)
#webbrowser.open('output.html')
answers = open("ocr2.txt", "rt")
ansTable = answers.read()
answers.close()
ans = ansTable.splitlines()
ans1 = str(ans[0])
ans2 = str(ans[2])
ans3 = str(ans[4])
print(' ')
print(newWord)
print(' ')
print(newString)
print(' ')
print("1. "+ans1)
print("2. "+ans2)
print("3. "+ans3)
ans1Score = 0
ans2Score = 0
ans3Score = 0
soup = BeautifulSoup(response.text, 'lxml')
for g in soup.find_all(class_='g'):
webBlock = str(g)
ans1Tally = webBlock.count(ans1)
ans2Tally = webBlock.count(ans2)
ans3Tally = webBlock.count(ans3)
if webBlock.find(ans1)!=-1:
ans1Score += ans1Tally
if webBlock.find(ans2)!=-1:
ans2Score += ans2Tally
if webBlock.find(ans3)!=-1:
ans3Score += ans3Tally
#print(ans1+": "+str(ans1Score))
#print(ans2+": "+str(ans2Score))
#print(ans3+": "+str(ans3Score))
#print('-----')
#print(g.text)
print(' ')
print('-----')
print(ans1+": "+str(ans1Score))
print(ans2+": "+str(ans2Score))
print(ans3+": "+str(ans3Score))
print('-----')
#print(g.text)
私は基本的に実装したいどのような(フレーズは、テキストのブロック内に見つからない場合ということですそれはタイトル、リンク、およびテキストの小さな宣伝文句が含まれているので、それは、Google検索の結果である私はwebBlockこれを印刷する場合、それが含まれているものの例です:。その場合
<div class="g"><h3 class="r"><a href="/url?q=https://www.rollingstone.com/glixel/news/lists/bloodbowl-and-beyond-the-most-absurdly-violent-football-games-of-all-time-w464896/mutant-league-football-w464904&sa=U&ved=0ahUKEwjSxdiFq5_YAhWlSd8KHSX-DtUQFghKMAk&usg=AOvVaw2e_Z2FBOjUfryPIGK3yhEq"><b>Mutant League</b> Football | 'Bloodbowl' and Beyond: The Most ...</a></h3><div class="s"><div class="kv" style="margin-bottom:2px"><cite>https://www.rollingstone.com/...<b>games</b>.../<b>mutant-league</b>-football-w464904</cite><div class="_nBb"><div aria-expanded="false" aria-haspopup="true" data-ved="0ahUKEwjSxdiFq5_YAhWlSd8KHSX-DtUQ7B0ISzAJ" onclick="google.sham(this);" style="display:inline" tabindex="0"><span class="_O0"></span></div><div class="am-dropdown-menu" role="menu" style="display:none" tabindex="-1"><ul><li class="_Ykb"><a class="_Zkb" href="/url?q=http://webcache.googleusercontent.com/search%3Fq%3Dcache:7JwZzaQGVBsJ:https://www.rollingstone.com/glixel/news/lists/bloodbowl-and-beyond-the-most-absurdly-violent-football-games-of-all-time-w464896/mutant-league-football-w464904%252BThe%2B%2522Mutant%2BLeague%2522%2Bvideo%2Bgames%2Bdid%2BNOT%2Binclude%2Bwhich%2Bsport%3F%26hl%3Den%26ct%3Dclnk&sa=U&ved=0ahUKEwjSxdiFq5_YAhWlSd8KHSX-DtUQIAhNMAk&usg=AOvVaw1l8NtEdw1gvsqGLNw13Ulf">Cached</a></li></ul></div></div></div><span class="st">Feb 3, 2017 <b>...</b> In which Electronic Arts realized they still had the durable Madden '93 engine <br/>
lying around, and decide to commit their talents to more than one football <b>game</b> <br/>
every year. <b>Mutant League</b> Football for the Sega Genesis is ridiculous . It takes <br/>
place in a post-apocalyptic wasteland where radiation has mottled ...</span><br/></div></div>
私が何をしたいですテキストブロックには文字列が含まれていないので、リンクを抽出して元のgoogle seで行ったことをしたいアーチリンク:
すべての< p>タグを収集するためにデータを解析し、そこにフレーズが含まれているかどうかを調べて、それをタリーに追加します。これは、すべてのフレーズでこれを実行する必要があります。私はに走った
いくつかの問題がある:すべてのリンクの
は、http含まれています://、https://でまたはWWW ...たとえば、mario.wikia.com。私はそれが何であってもリンクを引くことができるようにしたい。
私は今この問題に頭を悩ましているので、ここでそれを投げ捨てて誰か解決策があるかどうかを確かめました。私は単純な答えがあるかもしれないと確信していますが、私はPythonには新しく、たくさんの "瞬間"の瞬間を持っています。
とにかく、何か助けていただければ幸いです。読んでいただきありがとうございます!
ストリング」の場合"いくつかの文字列ではありません": \t \t dowork() – mysticcode
ええ、私の問題はWebBlockからURLを抽出する方法です。しかし、返信いただきありがとうございます! – DevinGP
'' ' Martin