0
このコードはオンラインであり、使用したいのですが、収集したデータをcsvファイルにエクスポートする方法が見つかりません。クロールからcsvファイルにデータをエクスポートしようとしています
import urllib
import scrapy
import json
import csv
from bs4 import BeautifulSoup
url = "http://www.straitstimes.com/tags/malaysia-crimes"
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)
# kill all script and style elements
for script in soup(["script", "style"]):
script.extract() # rip it out
# get text
text = soup.body.get_text()
# break into lines and remove leading and trailing space on each
lines = (line.strip() for line in text.splitlines())
# break multi-headlines into a line each
chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
# drop blank lines
text = '\n'.join(chunk for chunk in chunks if chunk)
print(text)
あなたが望む出力を投稿してください。 –
ここで答えを見てください:http://stackoverflow.com/questions/41985454/how-to-export-data-from-a-beautifulsoup-scrape-to-a-csv-file それは非常に似ています問題と答えはあなたを助けることができます。 –
[csvファイルにcleansoupスクレープからデータをエクスポートする方法]の可能な複製(http://stackoverflow.com/questions/41985454/how-to-export-data-from-a-beautifulsoup-scrape-to-a -csv-file) –