python爬虫get json解析

# -*-coding:utf8-*-
from lxml import etree
from multiprocessing.dummy import Pool as ThreadPool
import requests
import json
import sys

reload(sys)

sys.setdefaultencoding('utf-8')

'''''重新运行之前请删除content.txt,因为文件操作使用追加方式,会导致内容太多。'''


def towrite(contentdict):
    f.writelines(u'文章标题:' + str(contentdict['title']) + '\n')
    f.writelines(u'作者名称:' + str(contentdict['user_name']) + '\n')
    f.writelines(u'创建时间:' + str(contentdict['category']) + '\n')
    f.writelines(u'类别:' + str(contentdict['created_at']) + '\n\n')


def spider(url):
    print(url)
    html = requests.get(url)
    # print(html.text)
    # selector = etree.HTML(html.text)

    content_field = json.loads(html.text)

    articles = content_field['articles']
    print(len(articles))
    item = {}
    for each in articles:
        item['title'] = each['title']
        item['user_name'] = each['user_name']
        item['category'] = each['category']
        item['created_at'] = each['created_at']
        towrite(item)


if __name__ == '__main__':
    pool = ThreadPool(2)
    f = open('content.txt', 'a')
    page = []
    for i in range(0, 2):
        newpage = 'https://www.csdn.net/api/articles?type=more&category=home&shown_offset=0'
        page.append(newpage)

    results = pool.map(spider, page)
    pool.close()
    pool.join()
    f.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值