1
텍스트 마이닝 목적으로 여러 기사로 데이터베이스를 만들려고합니다. 웹 스크래핑을 통해 본문을 추출한 다음이 기사의 본문을 CSV 파일에 저장합니다. 그러나 모든 본문을 저장하지 못했습니다. 내가 작성한 코드는 마지막 URL (기사)에만 텍스트를 저장하는 반면, 내가 긁고있는 내용 (그리고 저장해야 할 내용)을 인쇄하면 모든 기사의 본문을 가져옵니다.csv 파일에 본문 내용 저장 | Python 3
import requests
from bs4 import BeautifulSoup
import csv
r=["http://www.nytimes.com/2016/10/12/world/europe/germany-arrest-syrian-refugee.html",
"http://www.nytimes.com/2013/06/16/magazine/the-effort-to-stop-the- attack.html",
"http://www.nytimes.com/2016/10/06/world/europe/police-brussels-knife-terrorism.html",
"http://www.nytimes.com/2016/08/23/world/europe/france-terrorist-attacks.html",
"http://www.nytimes.com/interactive/2016/09/09/us/document-Review-of-the-San-Bernardino-Terrorist-Shooting.html",
]
for url in r:
t= requests.get(url)
t.encoding = "ISO-8859-1"
soup = BeautifulSoup(t.content, 'lxml')
text = soup.find_all(("p",{"class": "story-body-text story-content"}))
print(text)
with open('newdb30.csv', 'w', newline='') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=' ',quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamwriter.writerow(text)
TypeError : 목록에만 연결 ("str"제외) 할 수 있습니다. 그래서 str (텍스트) + "\ r \ n"에서 편집합니다. 그리고 출력은 다음과 같이 작동합니다 : dt t - t o t l l c o u n t = "6 5 5 2"> M r. | | T h é v e n e t | | 나는 | | n o t | | s u r e | | w h e n | | - | | o r | | e v e n | | i f | 더 읽기 쉽게 만드는 방법에 대한 팁이 있습니까? –
for 루프 앞에'all_text = []'또는'all_text = ""'를 사용 했습니까? 그것은 두 번째이어야합니다. – yper
두 번째 방법을 제안 된 all_text = ""로 사용했습니다. str (텍스트)를 추가하여 오류를 해결했지만 출력을 읽기가 어려워졌습니다. –