【问题标题】:How to append scraped data efficiently into an array? (Python, BS4, Requests)如何有效地将抓取的数据附加到数组中? (Python、BS4、请求)
【发布时间】:2016-03-18 20:16:57
【问题描述】:

我是 python 新手,我正在尝试使用 BeautifulSoup 和 Request 提取天气数据。我有一个关于数组在 python 中的工作方式的问题,以及将抓取的数据插入到格式良好的结构中的最佳方法是什么。

    from lxml import html
    from bs4 import BeautifulSoup
    import requests

    url = "http://climate.weather.gc.ca/climateData/hourlydata_e.html?timeframe=1&Prov=ON&StationID=31688&hlyRange=2002-06-04|2016-03-17&Year=2016&Month=3&Day=15"
    r = requests.get(url)
    page = r.content
    soup = BeautifulSoup(page, "lxml")

    table = soup.find('table', class_='wet-boew-zebra span-8 ')
    rows = table.find_all('tr')

    data = []

   for item in rows:
        data.append(item.text)
   print data

我的代码在上面,主要是我遇到的麻烦是一开始我尝试做的:

    for item in rows:
        print(item.text)

我会看到所有的数据,因为它在项目中循环。然后我尝试这样做:

    for item in rows:
        data = (item.text)
    print data 

这只会显示表格最后一行的数据。然后我尝试附加(在上面的主代码中)并得到这样的东西:

[u"\n\n温度定义\xb0C\n露点温度定义\xb0C\nRel Hum定义%\n风向定义10's deg\n风速定义km/h\n能见度定义km\nStn压力定义kPa\nHmdx定义\n风寒定义\n天气定义\n", u' TIME ', u'\n00:00\n9.5\n6.3\n81\nLegendMM\nLegendMM\n\n99.38\n\n\nLegendNANA\n', u' \n01:00\n9.0\n6.4\n84\nLegendMM\nLegendMM\n\n99.33\n\n\nLegendNANA\n', u'\n02:00\n8.0\n6.5\n91 \nLegendMM\nLegendMM\n\n99.26\n\n\nLegendNANA\n', u'\n03:00\n7.6\n6.5\n92\nLegendMM\nLegendMM\n\n99.31\n\n \nLegendNANA\n', u'\n04:00\n7.2\n6.1\n93\nLegendMM\nLegendMM\n\n99.33\n\n\nLegendNANA\n', u'\n05:00\n7 .1\n6.2\n94\nLegendMM\nLegendMM\n\n99.35\n\n\nLegendNANA\n', u'\n06:00\n7.6\n6.8\n95\nLegendMM\nLegendMM\n \n99.39\n\n\nLegendNANA\n', u'\n07:00\n7.6\n6.8\n95\nLegendMM\nLegendMM\n\n99.44\n\n\nLegendNANA\n', u'\n08:00\n\n\n\n\n\n\n\n\n\n\n', u'\n09:00\n7.3\n6.0\n92\nLegendMM\nLegendMM \n\n99.49\n\n\nLegendNANA\n', u'\n10:00\n7.4\n6.3\n92\nLegendMM\nLegendM M\n\n99.57\n\n\nLegendNANA\n', u'\n11:00\n7.3\n5.5\n88\nLegendMM\nLegendMM\n\n99.62\n\n\nLegendNANA\ n', u'\n12:00\n7.7\n5.2\n84\nLegendMM\nLegendMM\n\n99.61\n\n\nLegendNANA\n', u'\n13:00\n7.9\ n4.6\n80\nLegendMM\nLegendMM\n\n99.63\n\n\nLegendNANA\n', u'\n14:00\n9.6\n5.3\n75\nLegendMM\nLegendMM\n\n99。 61\n\n\nLegendNANA\n', u'\n15:00\n10.0\n5.8\n75\nLegendMM\nLegendMM\n\n99.63\n\n\nLegendNANA\n', u'\ n16:00\n10.0\n5.1\n72\nLegendMM\nLegendMM\n\n99.65\n\n\nLegendNANA\n', u'\n17:00\n9.6\n4.7\n72\ nLegendMM\nLegendMM\n\n99.67\n\n\nLegendNANA\n', u'\n18:00\n8.6\n5.3\n80\nLegendMM\nLegendMM\n\n99.72\n\n\ nLegendNANA\n', u'\n19:00\n7.9\n4.1\n77\nLegendMM\nLegendMM\n\n99.82\n\n\nLegendNANA\n', u'\n20:00\n7. 9\n4.3\n78\nLegendMM\nLegendMM\n\n99.88\n\n\nLegendNANA\n', u'\n21:00\n7.7\n4.2\n79\nLegendMM\nLegendMM\n\ n99.89\n\n\nLegendNANA\n', u'\n22:00\n7.4\n4.0\n79\nLegendMM\nLegendMM\n\n99.86\n\n\nLegendNANA\n', u '\n23:00\n7.1\n4.0\n81\nLegendMM\nLegendMM\n\n99.85\n\n\nLegendNANA\n']

总而言之,将数据插入到数组中以便我可以轻松操作/分析我抓取的数据的最佳方法是什么?

【问题讨论】:

  • 您目前在上述数组中的方式有​​什么问题?
  • 你到底想做什么?当您附加到循环中时,您将所有内容都放在一个列表中,所以我不确定您想要什么
  • 我想分隔列表中的数据,以便它们在列中。您可以查看链接以查看表格。
  • 所以基本上你想重新创建表? stackoverflow.com/a/35920205/2141635
  • @PadraicCunningham 基本上是的,谢谢,这很有帮助。

标签: python arrays beautifulsoup python-requests


【解决方案1】:

使用 BeautifulSoup 返回的row 对象提取td 标签

for row in rows:
    data.append([c.text for c in row.find_all('td')])

`

【讨论】:

  • BeautifulSoup 返回的对象是否有参考文档?他们网站上的文档有点混乱。
  • 是的,一个对象被称为Tag——来自crummy.com/software/BeautifulSoup/bs4/doc 一个Tag对象对应于原始文档中的一个XML或HTML标签
猜你喜欢
  • 2018-01-22
  • 1970-01-01
  • 2021-06-04
  • 1970-01-01
  • 2015-09-28
  • 1970-01-01
  • 2013-11-28
  • 2012-05-02
  • 1970-01-01
相关资源
最近更新 更多