如何将for循环输出保存到数据框中的列

时间:2019-12-03 22:23:33

标签: 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', href=True)):
    print(link['href'])

1 个答案:

答案 0 :(得分:0)

您可以考虑将代码放入返回所需输出的函数中

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

没有更多关于您要尝试执行的操作的详细信息,很难提供更多指导