Twitter json数据,解析成mysql

时间:2017-09-24 17:18:26

标签: python json twitter mysql-python

我正在尝试使用来自json文件的python将数据插入MySQL,这是我从twitter中获取的。 我可以将数据存储在一个表中,而另一个表是空的。我想要的所有字段都存储在第一个表(推文)中,但第二个表(用户)是空的,而不会在运行程序时出现任何错误。

    db = MySQLdb.connect("localhost","root","","asif" )
cursor = db.cursor()

data = []
with open('@ImranKhanPTI\@ImranKhanPTI_2017-09-18_to_2017-09-24.json') as f:
    for line in f:
        data.append(json.loads(line))

for item in data:
    # tweet
    created_at = item['created_at']
    id = item['id']
    id_str = item['id_str']
    text = repr(item['text'])
    if len(text)>2:
        text = text[2:len(text)-1]
    newtext = text.replace('"','\'')
    truncated = item['truncated']
    sql1 = "insert into tweet(created_at, id, id_str, text, truncated) values ('%s',%d,'%s',\"%s\", %d);"%(created_at, id, id_str, newtext, truncated)
    cursor.execute(sql1)

    #user

    user__id = item['user']['id']
    user__name = item['user']['name']
    user__screen_name = item['user']['screen_name']
    user__location = item['user']['location']
    user__url = item['user']['url']
    user__protected = item['user']['protected']
    user__followers_count = item['user']['followers_count']
    user__friends_count = item['user']['friends_count']
    user__listed_count = item['user']['listed_count']
    user__created_at = item['user']['created_at']
    user__favourites_count = item['user']['favourites_count']
    user__statuses_count = item['user']['statuses_count']
    user__following = item['user']['following']
    user__follow_request_sent = item['user']['follow_request_sent']
    geo = item['geo']
    coordinates = item['coordinates']
    place = item['place']
    str = "select * from user where user__id = %d;" % (user__id)
    count = cursor.execute(str)
    if count == None:
        sql2 = "insert into user(user__id, user__name, user__screen_name, user__location, user__url, user__protected, " \
               "user__followers_count, user__friends_count, user__listed_count, user__created_at, user__favourites_count, " \
               "user__statuses_count, user__following, user__follow_request_sent, geo, coordinates, place) " \
               "values (%s,'%s','%s','%s', '%s', %d, %d, %d, %d, '%s', %d, %d, %d, %s, '%s', '%s', '%s');"%(user__id,
                user__name, user__screen_name, user__location, user__url, user__protected, user__followers_count,
                user__friends_count, user__listed_count, user__created_at, user__favourites_count,
                 user__statuses_count, user__following, user__follow_request_sent, geo, coordinates, place)
        cursor.execute(sql2)

这是json样本

"user": {
  "id": 147272410,
  "id_str": "147272410",
  "name": "MS Khan!",
  "screen_name": "Saleem82",
  "location": "Peshawar",
  "description": "",
  "url": null,
  "entities": {
    "description": {
      "urls": [

      ]
    }
  },
  "protected": false,
  "followers_count": 161,
  "friends_count": 275,
  "listed_count": 22,
  "created_at": "Sun May 23 17:40:55 +0000 2010",
  "favourites_count": 1226,
  "utc_offset": 18000,
  "time_zone": "Karachi",
  "geo_enabled": true,
  "verified": false,
  "statuses_count": 17982,
  "lang": "en",
  "contributors_enabled": false,
  "is_translator": false,
  "is_translation_enabled": false,
  "profile_background_color": "000000",
  "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png",
  "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png",
  "profile_background_tile": false,
  "profile_image_url": "http://pbs.twimg.com/profile_images/428092939277848577/8ObkVXXz_normal.jpeg",
  "profile_image_url_https": "https://pbs.twimg.com/profile_images/428092939277848577/8ObkVXXz_normal.jpeg",
  "profile_link_color": "DD2E44",
  "profile_sidebar_border_color": "000000",
  "profile_sidebar_fill_color": "000000",
  "profile_text_color": "000000",
  "profile_use_background_image": false,
  "has_extended_profile": false,
  "default_profile": false,
  "default_profile_image": false,
  "following": false,
  "follow_request_sent": false,
  "notifications": false,
  "translator_type": "none"
},

0 个答案:

没有答案