【问题标题】:python Getting table values from a websitepython从网站获取表值
【发布时间】:2017-04-01 17:17:42
【问题描述】:

我正在尝试从网站中提取特定的表值 (http://www.forexfactory.com/calendar.php?day=nov18.2016) 使用 Python BeautifulSoup

到目前为止的代码:

from bs4 import BeautifulSoup
from urllib.request import urlopen

content = urlopen("http://www.forexfactory.com/calendar.php?day=nov18.2016").read()
soup = BeautifulSoup(content, 'html.parser')

tables = soup.findAll("table")
for table in tables:
     if table.findParent("table") is None:
        print(table)

我可以打印所有多个表及其内容,但是如何获取名为“calendar__table”的特定表并通过它进行迭代以获取每一行及其值?

【问题讨论】:

    标签: python beautifulsoup lxml


    【解决方案1】:
    import requests
    from bs4 import BeautifulSoup
    
    r = requests.get('http://www.forexfactory.com/calendar.php?day=nov18.2016')
    soup = BeautifulSoup(r.text, 'lxml')
    
    calendar_table = soup.find('table', class_="calendar__table")
    for row in calendar_table.find_all('tr', class_=['calendar__row calendar_row','newday']):
        row_data = [td.get_text(strip=True) for td in row.find_all('td')]
        print(row_data)
    

    出来:

    ['FriNov 18', '2:00am', 'EUR', '', 'German PPI m/m', '', '', '0.3%', '-0.2%', '']
    ['', '3:30am', 'EUR', '', 'ECB President Draghi Speaks', '', '', '', '', '']
    ['', '4:00am', 'EUR', '', 'Current Account', '', '', '31.3B', '29.7B', '']
    ['', '4:10am', 'GBP', '', 'MPC Member Broadbent Speaks', '', '', '', '', '']
    ['', '5:30am', 'CHF', '', 'Gov Board Member Maechler Speaks', '', '', '', '', '']
    ['', '8:30am', 'CAD', '', 'Core CPI m/m', '', '', '0.3%', '0.2%', '']
    ['', '9:30am', 'USD', '', 'FOMC Member Dudley Speaks', '', '', '', '', '']
    ['', '10:00am', 'USD', '', 'CB Leading Index m/m', '', '', '0.1%', '0.2%', '']
    ['', '9:45pm', 'USD', '', 'FOMC Member Powell Speaks', '', '', '', '', '']
    

    【讨论】:

      【解决方案2】:

      你可以像这样在搜索中传递参数:

      tables = soup.findAll("table", {'class':'calendar__table'})
      

      然后你可以迭代槽表 -> 行 -> 单元格:

      for table in tables:
          for row in table.findAll("tr"):
              for cell in row.findAll("td"):
                  print(cell.text, end = ' ' )
              print()
      

      【讨论】:

      • 这很有效,感谢 Yevhen。我如何迭代此表中的每一行?
      • 谢谢叶文。有没有办法可以创建一个 dom 树表格?
      猜你喜欢
      • 1970-01-01
      • 2014-08-17
      • 1970-01-01
      • 2017-11-26
      • 2022-08-18
      • 2020-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多