NYTimesの記事の内容を抽出し、特定の単語を数えるために文字列に入れようとしています。すべての記事の内容はHTMLの 'p'タグで検索されます。私はパラグラフに(コード内のコメント)一つ一つを取得することができるが、私は次のエラーを取得しておくので、私は、変数段落を反復処理することができません。ここでPython + BeautifulSoupニューヨークタイムズWeb記事スクレイピング
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-52-ccc2f7cf5763> in <module>()
16
17 for i in paragraphs:
---> 18 article = article + paragraphs[i].get_text()
19
20 print(article)
TypeError: list indices must be integers, not Tag
はコードです:
import requests
from bs4 import BeautifulSoup
session = requests.Session()
url = "http://www.nytimes.com/2015/01/02/world/europe/turkey-police-thwart-attack-on-prime-ministers-office.html"
req = session.get(url)
soup = BeautifulSoup(req.text)
paragraphs = soup.find_all('p', class_='story-body-text story-content')
#article = paragraphs[0].get_text()
#article = article + paragraphs[1].get_text()
#article = article + paragraphs[2].get_text()
#article = article + paragraphs[3].get_text()
#article = article + paragraphs[4].get_text()
#article = article + paragraphs[5].get_text()
#article = article + paragraphs[6].get_text()
for i in paragraphs:
article = article + paragraphs[i].get_text()
print(article)
ご協力いただきありがとうございます。私はエコノミストであり、コーディング方法を学び始めました。この問題を解決するために忍耐力を持ってくれてありがとう。
は、あなたがより多くのために彼らの記事を使用している場合は特に、あまりにもサービスのNYTの利用規約を確認することを忘れないでください学習運動。 –