Python 如何将循环输出保存到数据帧中的列

Python 如何将循环输出保存到数据帧中的列,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,如何将此python脚本的输出保存为数据帧 import requests from bs4 import BeautifulSoup, SoupStrainer response = requests.get('https://webscraper.io/test-sites/e-commerce/allinone') for link in BeautifulSoup(response.content, "html.parser", parse_only=SoupStrainer('a

如何将此python脚本的输出保存为数据帧

import requests
from bs4 import BeautifulSoup, SoupStrainer


response = requests.get('https://webscraper.io/test-sites/e-commerce/allinone')

for link in BeautifulSoup(response.content, "html.parser", parse_only=SoupStrainer('a', href=True)):
    print(link['href'])

您可以考虑将代码放入返回期望输出

的函数中。
def myfunction(response = response, parser = "html.parser", ...):
    '''
    this function saves output
    '''
    ....     
     return(output)
或者可以在循环中定义列表和索引

mylist = []

i = 1

for link in BeautifulSoup(response.content, "html.parser", parse_only=SoupStrainer('a', href=True)):
    mylist[i] = link['href']
    i += 1

如果没有更多关于您正试图做什么的细节,就很难提供更多的指导

谢谢!最后使用list方法将每个循环附加到列表中,然后将列表转换为数据帧。虽然您的代码不起作用,但方法确实起作用。我将print语句替换为mylist.append((anchor['href']))