Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在字典中存储for循环的每个输出_Python_Python 3.x_For Loop_Beautifulsoup - Fatal编程技术网

Python 如何在字典中存储for循环的每个输出

Python 如何在字典中存储for循环的每个输出,python,python-3.x,for-loop,beautifulsoup,Python,Python 3.x,For Loop,Beautifulsoup,我不知道如何将所有的输出值都输入到dict中。当我运行代码时,我只得到最后一个输出,但是如果我在for循环中使用print进行测试,我可以看到我正在收集更多的输出 我的目标是获得所有的输出,而不仅仅是最后一个 代码: def cointelegraph(): for div in soup.find_all('div', class_='post-preview-item-inline'): content = dict() title = div.fin

我不知道如何将所有的输出值都输入到dict中。当我运行代码时,我只得到最后一个输出,但是如果我在for循环中使用print进行测试,我可以看到我正在收集更多的输出

我的目标是获得所有的输出,而不仅仅是最后一个

代码:

def cointelegraph():
    for div in soup.find_all('div', class_='post-preview-item-inline'):
        content = dict()
        title = div.find(class_='post-preview-item-inline__title').get_text()
        #print(title)
        content['title'] = title
        body = div.find(class_='post-preview-item-inline__text').get_text()
        #print(body)
        content['body'] = body
        for links in div.select('.post-preview-item-inline__figure-link'):
            link = links.get('href')
            #print(link)
            content['link'] = link
            content['total_body'] = body + '\n \n' +' Link to article: ' + link

    return content


#make_post(cointelegraph())
print(cointelegraph())
{'title': ' Bitcoin Price Reclaims $10K Reversing Weekend Losses, XTZ Soars 13% ', 'body': ' Bitcoin price briefly reclaimed $10K after a bearish weekend which saw the creation of a new CME gap at $10,460. ', 'link': 'https://cointelegraph.com/news/bitcoin-price-reclaims-10k-reversing-weekend-losses-xtz-soars-13', 'total_body': ' Bitcoin price briefly reclaimed $10K after a bearish weekend which saw the creation of a new CME gap at $10,460. \n \n Link to article: https://cointelegraph.com/news/bitcoin-price-reclaims-10k-reversing-weekend-losses-xtz-soars-13'}
结果:

def cointelegraph():
    for div in soup.find_all('div', class_='post-preview-item-inline'):
        content = dict()
        title = div.find(class_='post-preview-item-inline__title').get_text()
        #print(title)
        content['title'] = title
        body = div.find(class_='post-preview-item-inline__text').get_text()
        #print(body)
        content['body'] = body
        for links in div.select('.post-preview-item-inline__figure-link'):
            link = links.get('href')
            #print(link)
            content['link'] = link
            content['total_body'] = body + '\n \n' +' Link to article: ' + link

    return content


#make_post(cointelegraph())
print(cointelegraph())
{'title': ' Bitcoin Price Reclaims $10K Reversing Weekend Losses, XTZ Soars 13% ', 'body': ' Bitcoin price briefly reclaimed $10K after a bearish weekend which saw the creation of a new CME gap at $10,460. ', 'link': 'https://cointelegraph.com/news/bitcoin-price-reclaims-10k-reversing-weekend-losses-xtz-soars-13', 'total_body': ' Bitcoin price briefly reclaimed $10K after a bearish weekend which saw the creation of a new CME gap at $10,460. \n \n Link to article: https://cointelegraph.com/news/bitcoin-price-reclaims-10k-reversing-weekend-losses-xtz-soars-13'}

您使用的数据结构和位置错误。试试下面的方法

def cointelegraph():
    all_content = []

    for div in soup.find_all('div', class_='post-preview-item-inline'):
        content = dict()
        title = div.find(class_='post-preview-item-inline__title').get_text()
        #print(title)
        content['title'] = title
        body = div.find(class_='post-preview-item-inline__text').get_text()
        #print(body)
        content['body'] = body
        content['links'] = []
        for links in div.select('.post-preview-item-inline__figure-link'):
            link = links.get('href')
            #print(link)
            content['links'].append({"link": link, 'total_body': body + '\n \n' +' Link to article: ' + link})

        all_content.append(content)
    return all_content