TypeError:“ ResultSet”对象不可调用-带有BeautifulSoup的Python

Fun*_*keh -1 html python beautifulsoup

python的新增功能,在尝试设置一些代码以从网页列表中抓取数据时会遇到错误。

这些页面之一的链接是-https://rspo.org/members/2.htm ,我正在尝试获取诸如“会员编号”,“类别”,“部门”,“国家”,等等,然后将其全部导出到电子表格中。

码:

from bs4 import BeautifulSoup as soup
from urllib.request import urlopen
import requests

pages = []

for i in range(1, 10):
    url = 'https://rspo.org/members/' + str(i)
    pages.append(url)


for item in pages:
    page = requests.get(item)
    soup = soup(page.text, 'html.parser')
    member = soup.find_all("span", {"class":"current"})
Run Code Online (Sandbox Code Playgroud)

我得到以下错误:

追溯(最近一次通话):

文件“”,第3行,在汤=汤中(page.text,'html.parser')

TypeError:“ ResultSet”对象不可调用

不知道为什么我得到这个错误。我尝试查看Stack Overflow上的其他页面,但似乎没有任何类似的错误出现在上面。

Bra*_*ney 5

问题是您有名称冲突,因为您以多种方式使用同一名称。因此,您soup将其设置为BeautifulSoup汤对象,但随后又被用作同一对象。

尝试以下方法:

from bs4 import BeautifulSoup
from urllib.request import urlopen
import requests

pages = []

for i in range(1, 10):
    url = 'https://rspo.org/members/' + str(i)
    pages.append(url)


for item in pages:
    page = requests.get(item)
    soup = BeautifulSoup(page.text, 'html.parser')
    member = soup.find_all("span", {"class":"current"})
Run Code Online (Sandbox Code Playgroud)

请注意,我只是从中删除了别名BeautifulSoup。我采用这种方法的原因很简单。Python中的标准约定是类应为适当大小写。即ClassOneBeautifulSoup。类的实例应为小写,例如classand soup。这有助于避免名称冲突,但也可以使您的代码更直观。一旦了解了这一点,就可以轻松阅读代码和编写简洁的代码。