如何将抓取的数据导出为CSV文件。我的代码正确打印出所有数据,但我想以同样的方式将其导出到CSV文件中。
如何将数据写入csv文件?
import requests
import json
import csv
with open('Links.csv', 'r') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in readCSV:
data = row[0]
for b in row:
r = requests.get(b)
json_object = json.loads('{"data":%s}}' % (r.content.decode("utf-8").replace("jQuery111002521088376353553_1491736907010(", "")[:-2].replace("\'", "")))
for game in json_object["data"]["docs"]:
print ("Name: %s, Price: %s, CatalogId: %s, slug: %s" % (game["name"], game["minPrice"], game["catalogId"], game["slug"]))
答案 0 :(得分:0)
我认为熊猫是你正在寻找的包裹。 使用pandas.dataframe.from_dict或pandas.dataframe.from_json。一旦你有了你的pandas数据框,就可以轻松地编写csv文件了。 https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html
答案 1 :(得分:0)
您可以将数据写入csv文件中的一行,如:
java.lang.SecurityException: Package xxx.xxx.test does not belong to 10095
at android.os.Parcel.readException(Parcel.java:2004)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.call(ContentProviderNative.java:651)
at android.provider.Settings$NameValueCache.getStringForUser(Settings.java:1924)
at android.provider.Settings$Global.getStringForUser(Settings.java:10362)
at android.provider.Settings$Global.getString(Settings.java:10351)
at android.provider.Settings$Global.getFloat(Settings.java:10695)
at android.support.test.espresso.base.DefaultFailureHandler.getGlobalSetting(DefaultFailureHandler.java:178)
at android.support.test.espresso.base.DefaultFailureHandler.getSetting(DefaultFailureHandler.java:154)
at android.support.test.espresso.base.DefaultFailureHandler.getTransitionAnimationScale(DefaultFailureHandler.java:124)
at android.support.test.espresso.base.DefaultFailureHandler.isAnimationAndTransitionDisabled(DefaultFailureHandler.java:112)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:69)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:52)
at xxx.xxx.test.instruments.failure.ScreenshotFailureHandler.handle(ScreenshotFailureHandler.java:36)
at android.support.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:312)
at android.support.test.espresso.ViewInteraction.desugaredPerform(ViewInteraction.java:167)
at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:110)
at android.support.test.espresso.DataInteraction$DisplayDataMatcher$1.apply(DataInteraction.java:206)
at android.support.test.espresso.DataInteraction$DisplayDataMatcher$1.apply(DataInteraction.java:203)
at android.support.test.espresso.DataInteraction$DisplayDataMatcher.<init>(DataInteraction.java:223)
at android.support.test.espresso.DataInteraction$DisplayDataMatcher.<init>(DataInteraction.java:198)
at android.support.test.espresso.DataInteraction$DisplayDataMatcher.displayDataMatcher(DataInteraction.java:241)
at android.support.test.espresso.DataInteraction.makeTargetMatcher(DataInteraction.java:143)
at android.support.test.espresso.DataInteraction.check(DataInteraction.java:137)
at xxx.xxx.test.steps.ChangeServerUrlSteps.checkSettingsSaved(ChangeServerUrlSteps.java:112)
at xxx.xxx.test.steps.Prerequisites.serverConfigPrerequisites(Prerequisites.java:38)
at ✽.Given I am connected (features/001_login.feature:8)
这是添加到您的代码中的,需要初始化代码:
writeCSV.writerow([game["name"], game["minPrice"], game["catalogId"], game["slug"]])
你没有提供任何实际数据,所以我无法对此进行测试,但它会很接近。