【问题标题】:Loading scraped data into list将抓取的数据加载到列表中
【发布时间】:2018-09-18 16:57:56
【问题描述】:

我能够成功地从网站上抓取一些文本,现在我正在尝试将文本加载到列表中,以便稍后将其转换为 Pandas DataFrame。

该网站以 scsv 格式提供数据,因此可以快速获取。

以下是我的代码:

import requests
from bs4 import BeautifulSoup

#Specify the url:url
url = "http://rotoguru1.com/cgi-bin/fyday.pl?week=1&year=2017&game=dk&scsv=1"

# Packages the request, send the request and catch the response: r
r = requests.get(url)

#Extract the response:html_doc
html_doc = r.text


soup = BeautifulSoup(html_doc,"html.parser")

#Find the tags associated with the data you need, in this case
# it's the "pre" tags


for data in soup.find_all("pre"):
    print(data.text)

样本输出

周;年;GID;姓名;职位;团队;h/a;Oppt;DK积分;DK薪水 1;2017;1254;Smith, Alex;QB;kan;a;nwe;34.02;5400 1;2017;1344;Bradford, Sam;QB;min;h;nor;28.54;5900

【问题讨论】:

  • 代码不适合我
  • 对此深表歉意,我不确定出了什么问题。我正在运行 Python 3.6。我可以将确切的代码复制并粘贴到我的 IDE 中,然后它就会运行。
  • 不用担心,您使用的是什么 IDE?
  • 我正在使用 Spyder,它是最新 Anaconda 发行版附带的版本。
  • 我也在使用 Spyder。代码运行但没有输出。

标签: python-3.x beautifulsoup python-requests


【解决方案1】:

使用open函数写入csv文件

import requests
from bs4 import BeautifulSoup
url = "http://rotoguru1.com/cgi-bin/fyday.pl?week=1&year=2017&game=dk&scsv=1"
r = requests.get(url)
html_doc = r.content
soup = BeautifulSoup(html_doc,"html.parser")
file = open(“data.csv”,”w”)
for data in soup.find("pre").text.split('\n'):
    file.write(data.replace(';',','))
file.close()

【讨论】:

    【解决方案2】:

    这是您可以做的一件事,尽管有可能比我更了解熊猫的人可以提出更好的建议。

    你有r.text。把它放到一个方便的文本文件中,我叫它temp.csv。现在您可以使用pandas read_csv 方法将这些数据放入数据帧中。

    >>> df = pandas.read_csv('temp.csv', sep=';')
    

    附录:

    假设results 是这样的。

    >>> results = [['a', 'b', 'c'], [1,2,3], [4,5,6]]
    

    然后你可以用这种方式把它们放在一个数据框中。

    >>> df = pandas.DataFrame(results[1:], columns=results[0])
    >>> df
       a  b  c
    0  1  2  3
    1  4  5  6
    

    【讨论】:

    • 感谢您的反馈比尔。我确实考虑过。我希望将结果保存为列表,然后将列表转换为 DataFrame。目前代码返回的是 而不是一个列表,我正试图弄清楚如何修复该部分。
    • 好的,布赖恩。你的意思是说每一行都应该变成一个列表,其元素是最初用分号分隔的片段吗?
    • 您可能还希望将列表中的数字项从字符串转换为它们的数值...
    • 是的,这正是我的意思。我假设我需要创建一个包含空列表的变量,然后编写一个循环遍历每一行并将其附加为新行。
    • 是的,用results.extend(data.text.split(';')) 替换最后一行,将results=[] 放在for 循环之前。这会给你一个列表列表。
    【解决方案3】:

    如果您想将现有代码转换为列表,使用 split 方法可能会完成这项工作,然后使用 pandas 将其转换为数据框。

    import requests
    from bs4 import BeautifulSoup
    
    #Specify the url:url
    url = "http://rotoguru1.com/cgi-bin/fyday.pl?week=1&year=2017&game=dk&scsv=1"
    
    # Packages the request, send the request and catch the response: r
    r = requests.get(url)
    
    #Extract the response:html_doc
    html_doc = r.text
    
    
    soup = BeautifulSoup(html_doc,"html.parser")
    
    #Find the tags associated with the data you need, in this case
    # it's the "pre" tags
    
    
    for data in soup.find_all("pre"):
        print(data.text.split(";"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      • 2015-06-06
      • 1970-01-01
      • 2019-07-06
      • 2019-05-21
      • 2023-01-11
      相关资源
      最近更新 更多