【发布时间】:2020-10-09 03:45:25
【问题描述】:
我正在尝试通过使用 BeautifulSoup4 抓取本地 HTML 文件来收集一些数据。问题是,我试图获取的信息位于具有相同类标签的不同行上。我不确定如何访问它们。以下 html 屏幕截图包含我正在访问的两行,其中突出显示了我需要的数据(敏感信息被潦草写出)。
我目前的代码是:
def find_data(fileName):
with open(fileName) as html_file:
soup = bs(html_file, "lxml")
hline1 = soup.find("td", class_="headerTableEntry")
hline2 = hline1.find_next_sibling("td")
hline3 = hline2.find_next_sibling("td")
hline4 = hline3.find_next_sibling("td", class_="headerTableEntry")
line1 = hline1.text
line2 = hline2.text
line3 = hline3.text
#Nothing yet for lines 4,5,6
前 3 行效果很好,并给出了应有的 13、39 和 33.3%。但是对于第 4 行(应该是第二个标签和第一个标签,class=headerTableEntry),我得到一个错误“'NoneType' object is not callable”。
我的问题是,是否有不同的方法可以访问所有 6 个数据单元格,或者有没有办法编辑我编写第 4 行的工作方式?感谢您的帮助,非常感谢!
【问题讨论】:
-
请用实际的 html 编辑您的问题,而不是图像。
标签: python html python-3.x beautifulsoup typeerror