2016-11-04 4 views
0

私はCSVファイルに集められた複数のURLから記事を抽出しようとしています。私は出力を印刷するとき は、しかし、私はこのエラーを取得する: InvalidSchema:いいえ、接続アダプタは「[」http://www.nytimes.com/2016/10/06/world/europe/police-brussels-knife-terrorism.html「]」が見つかりませんでした|接続アダプタが見つかりませんでした。 Python 3.5 |リクエスト

import csv 
import requests 
from bs4 import BeautifulSoup 

with open('Training_news.csv', newline='') as file: 
    reader= csv.reader (file, delimiter=' ') 
    for row in reader: 
     r=requests.get(row) 
     r.encoding = "ISO-8859-1" 
     soup = BeautifulSoup(r.content, 'lxml') 
     text = soup.find_all(("p",{"class": "story-body-text story-content"})) 

私は、私はそれを印刷するとき、問題は「行」であると思いますCSVファイル内のすべてのURLを持つ単一のリストが、ファイルの任意の単一の値のためのリストを得ることはありません。 [「http://www.nytimes.com/2016/10/06/world/europe/police-brussels-knife-terrorism.html 『] [』 http://www.nytimes.com/2016/06/29/world/europe/turkey-istanbul-airport-explosions.html」]

答えて

0

rowがリストです。 requests.getには文字列が必要です。次のようにして、各行のアイテムを繰り返します:

with open('Training_news.csv', newline='') as file: 
    reader= csv.reader (file, delimiter=' ') 
    for row in reader: 
     for url in row: 
      r=requests.get(url) 
      r.encoding = "ISO-8859-1" 
      soup = BeautifulSoup(r.content, 'lxml') 
      text = soup.find_all(("p",{"class": "story-body-text story-content"})) 
関連する問題