Python2.7:不知道如何用BeautifulSoup4解析树

2024-04-25 07:45:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图解析this site来创建5个列表,每天一个,每个公告填充一个字符串。例如

[in]   custom_function(page)

[out]  [[<MONDAYS    ANNOUNCEMENTS>],
        [<TUESDAYS   ANNOUNCEMENTS>],
        [<WEDNESDAYS ANNOUNCEMENTS>],
        [<THURSDAYS  ANNOUNCEMENTS>],
        [<FRIDAYS    ANNOUNCEMENTS>]]

但我想不出正确的方法。你知道吗

这就是我目前所拥有的

from bs4 import BeautifulSoup
import requests
import datetime

url = http://mam.econoday.com/byweek.asp?day=7&month=4&year=2014&cust=mam&lid=0




# Get the text of the webpage
r               = requests.get(url)
data            = r.text
soup            = BeautifulSoup(data)


full_table_1 = soup.find('table', 'eventstable')

ScreenShot of Website Developers Tools

我发现我想要的是突出显示的标签,但我不知道如何找到确切的标签,然后将时间/公告解析成一个列表。我试过多种方法,但总是越来越混乱。你知道吗

我该怎么办?你知道吗


Tags: the方法textimporturl列表datatable
1条回答
网友
1楼 · 发布于 2024-04-25 07:45:30

其思想是找到所有具有events类的td元素,然后读取其中的div元素:

data = []
for day in soup.find_all('td', class_='events'):
    data.append([div.text for div in day.find_all('div', class_='econoevents')])

print data

印刷品:

[[u'Gallup US Consumer Spending Measure8:30 AM\xa0ET',
  u'4-Week Bill Announcement11:00 AM\xa0ET',
  u'3-Month Bill Auction11:30 AM\xa0ET',
  ...
 ],
 ...
]

相关问题 更多 >