在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python/ 如何把多個for循環(huán)的多個循環(huán)內(nèi)容分別組合起來?

如何把多個for循環(huán)的多個循環(huán)內(nèi)容分別組合起來?

# -*- coding:utf-8 -*-
import requests,re,time


url = 'https://www.toutiao.com/c/user/article/?page_type=1&user_id=67287555792&max_behot_time=0&count=20&as=A1E5AAE6A908C3F&cp=5A69784C438F5E1&_signature=OnHSARAdYB7GCDh9vrFywDpx0h'
q_url = 'https://www.toutiao.com/'
header = {
    'referer':'https://www.toutiao.com/c/user/67287555792/',
    'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36',
    'x-requested-with':'MLHttpRequest'
}
content = requests.get(url , headers = header).text
con = content.encode('utf-8').decode('unicode_escape')

def msg():
    title = re.findall('"title": "(.*?)",',con,re.S)
    times = re.findall('"behot_time": (.*?),',con,re.S)
    read = re.findall('"go_detail_count": (.*?),',con,re.S)
    con_url = re.findall('"source_url": "(.*?)"',con,re.S)
    source_url = re.findall('"display_url": "http://(.*?)"',con,re.S)
    for i in range(1,21):
        for t in title:
            print(t)
        for m in times:
            print(m)
        for r in read:
            print(r)
        for c in con_url:
            cc = q_url + c
            print(cc)
        for s in source_url:
            print(s)
        con_print = '標(biāo)題:' + t + '\n' + '時間:' + m + '\n' + '閱讀量:' + r + '\n' + '鏈接:' + cc + '\n' '原文鏈接:' + s
        print(con_print)

    #print('標(biāo)題:' + t + '\n' + '發(fā)布時間:' + m + '\n' '閱讀量:' + r + '\n' '鏈接:' + c + '\n' + '原文鏈接:' + s)
#rint(con)
msg()

想實現(xiàn)的效果是這樣的:

標(biāo)題:年關(guān)難過,“被催族”的“悲催”何人能懂?
時間:1516864032
閱讀量:8
鏈接:https://www.toutiao.com//item/6514899406209155591/
原文鏈接:www.123.com.cn/ydzx/128-536175.html

可是實際print(con_print)的時候是先把每一條都打印出來,然后用最后一條組合成。

怎么樣讓輸出的結(jié)果是這種呢,就是標(biāo)題、時間、閱讀等都循環(huán)一遍,然后組合好,再進(jìn)行下一次循環(huán),在組合再循環(huán)...

標(biāo)題:年關(guān)難過,“被催族”的“悲催”何人能懂?
時間:1516864032
閱讀量:8
鏈接:https://www.toutiao.com//item/6514899406209155591/
原文鏈接:www.123.com.cn/ydzx/128-536175.html
回答
編輯回答
生性

別用response.text,換成response.json(),你就可以操作dict形式的結(jié)果了。

就像下面這樣的:

{
    "login_status": false,
    "has_more": true,
    "next": {
        "max_behot_time": 1516455912
    },
    "page_type": 1,
    "message": "success",
    "data": [{
        "image_url": "http://p1.pstatp.com/list/190x124/5b4b0005fed9c63a1454",
        "abstract": "太陽能發(fā)電站雜草叢生很難清理,中國工程師:放10000只羊就行",
        "tag": "news",
        "tag_url": "video",
        "title": "太陽能發(fā)電站雜草叢生很難清理,中國工程師:放10000只羊就行",
        "has_video": true,
        "chinese_tag": "視頻",
        "source": "超級工程",
        "comments_count": 3745,
        "media_url": "/m1570427113937921/",
        "go_detail_count": 532127,
        "detail_play_effective_count": 761650,
        "source_url": "/item/6514979850942415364/",
        "item_id": "6514979850942415364",
        "article_genre": "video",
        "display_url": "http://toutiao.com/group/6514979850942415364/",
        "behot_time": 1516886952,
        "group_id": "6514979850942415364", 
        ...
    }, ... ],
    "is_self": false
}
2017年5月27日 01:15
編輯回答
舊酒館

可以用zip函數(shù),還可以看看itertools里面的zip_longest函數(shù)

a=list(range(5))
b=list('abcde')
for i in zip(a,b):
    print(i)
#輸出:
(0, 'a')
(1, 'b')
...
#稍作修改成你想要的結(jié)果
for i,j in zip(a,b):
    print(str(i)+'\n'+j) #這里str(i)是因為整數(shù)i是不能和字符串相加的
2018年1月29日 12:06