我正在尝试使用beautifulsoup从网页上抓取数据,并将其(最终)输出到csv中。作为第一步,我尝试获取相关表的文本。我设法做到了,但是当我重新运行它时,代码不再为我提供相同的输出:在运行for循环时,它不会保存所有12372条记录,而是仅保存最后一条。
我的代码的缩写版本是:
from bs4 import BeautifulSoup
BirthsSoup = BeautifulSoup(browser.page_source, features="html.parser")
print(BirthsSoup.prettify())
# this confirms that the soup has captured the page as I want it to
birthsTable = BirthsSoup.select('#t2 td')
# selects all the elements in the table I want
birthsLen = len(birthsTable)
# birthsLen: 12372
for i in range(birthsLen):
print(birthsTable[i].prettify())
# this confirms that the beautifulsoup tag object correctly captured all of the table
for i in range(birthsLen):
birthsText = birthsTable[i].getText()
# this was supposed to compile the text for every element in the table
但是for循环仅保存表中最后一个元素(即12372nd)的文本。我是否需要做其他事情以使它在循环通过时保存每个元素?我认为我先前的(期望的)输出在换行符中包含了每个元素的文本。
这是我第一次使用python,如果我犯了一个明显的错误,因此深表歉意。
答案 0 :(得分:6)
您正在做的是在每次迭代中覆盖您的birthText字符串,因此到结束时,只会保存最后一个。要解决此问题,请创建一个列表并添加每行:
birthsLen = len(birthsTable)
birthsText = []
for i in range(birthsLen):
birthsText.append(birthsTable[i].getText())
或更简洁地说:
birthsText = [line.getText() for line in birthsTable]