0
メインページの一連のハイパーリンクからテキストを抜き取り、その結果を文字列オブジェクトのリストとして保存しようとしています。私が書いたコードは個々のリンク上で実行すると動作しますが、すべてのリンクをループしようとすると分解します。Python Web Scraping、ループを分割する
FYI、私のベースURLは次のようになります。
base_url = "http://www.achpr.org"
そして、私のハイパーリンクは、次のようになります。
hyperlinks = ['/sessions/58th',
'/sessions/58th/resolutions/337/',
'/sessions/58th/resolutions/338/',
'/sessions/58th/resolutions/339/', ...]
だから、これは正常に動作します:
r = requests.get('http://www.achpr.org' + "/sessions/19th-eo/resolutions/328/")
soup = BeautifulSoup(r.text, "lxml")
soup.find('b').span.string
text = soup.findAll('span')
y = []
for i in text:
x = i.strings #returns string within tags
y.extend(x)
y = "".join(y)
y = y.replace("\n", " ")
y = y.replace("\xa0*", " ")
print(ok)
しかし、私はしてみてくださいこれをループにする:
output = []
for item in hyperlinks:
r = requests.get('http://www.achpr.org' + link)
soup = BeautifulSoup(r.text, "lxml")
soup.find('b').span.string
text = soup.findAll('span')
y = []
for i in text:
x = i.strings #returns string within tags (so no tags)
y.extend(x)
y = "".join(y)
y = y.replace("\n", " ")
y = y.replace("\xa0*", " ")
output.extend(y)
私は次のエラーを取得する:
あまりにも長い間、私は(間違った場所にインデントを入れて)本当に簡単ループエラーを作ってるんだが、私はこれを見つめてきたように感じます新鮮な目が欲しいです。誰でも私が間違っていることを見つけることができますか?
ああありがとう!とても簡単。 – chickpeaze