美文网首页
python ajax weibo

python ajax weibo

作者: Al_不期而遇 | 来源:发表于2019-01-13 12:23 被阅读7次

    import requests

    from urllib.parse import urlencode

    from pyquery import PyQuery as pq

    base_url = 'https://m.weibo.cn/api/container/getIndex?'

    headers = {

    'Host':'m.weibo.cn',

    'Refere':'https://m.weibo.cn/u/2830678474',

    'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:59.0) Gecko/20100101 Firefox/59.0',

    'X-Requested-With':'XNLHttpRequest',

    }

    def get_page(page):

    params = {

    'type' : 'uid',

    'value' : '2830678474',

    'containerid' : '1076032830678474',

    'page' : page

    }

    url = base_url + urlencode(params)

    try:

    response = requests.get(url, headers=headers)

    if response.status_code == 200:

    return response.json()

    except requests.ConnectionError as e:

    print('Error', e.args)

    def parse_page(json):

        if json:

            items = json.get('data').get('cards')

            for item in items:

                item = item.get('mblog')

                weibo = {}

    weibo['id'] = item.get('id')

                weibo['text'] = pq(item.get('text')).text()

                weibo['attitudes'] = item.get('attitudes_count')

                weibo['comments'] = item.get('comments_count')

                weibo['reposts'] = item.get('reposts_count')

                yield weibo

    if __name__ == '__main__':

    for page in range(1, 87):

    json = get_page(page)

    # results = parse_page(page)

    results = parse_page(json)

    for result in results:

    print(result)

    相关文章

      网友评论

          本文标题:python ajax weibo

          本文链接:https://www.haomeiwen.com/subject/qccuuftx.html