BeautifulSoup'NoneType'对象不可调用

osk*_*osk 0 python beautifulsoup

我正在尝试这样做:

req = urllib.request.Request("http://en.wikipedia.org/wiki/Philosophy")
content = urllib.request.urlopen(req).read()
soup = bs4.BeautifulSoup(content, "html.parser")
content = strip_brackets(soup.find('div', id="bodyContent").p)

for link in bs4.BeautifulSoup(content, "html.parser").findAll("a"):
    print(link.get("href"))
Run Code Online (Sandbox Code Playgroud)

如果我改为这样做循环:

for link in soup.findAll("a"):
    print(link.get("href"))
Run Code Online (Sandbox Code Playgroud)

我不再遇到错误,但是我想先除去内容的括号,然后再获得内容的所有链接。

错误(第36行是for循环的行):

Traceback (most recent call last):
  File "....py", line 36, in <module>
    for link in bs4.BeautifulSoup(content, "html.parser").findAll("a"):
  File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\bs4\__init__.py", line 191, in __init__
    markup = markup.read()
TypeError: 'NoneType' object is not callable
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Dmi*_*kiy 5

您的最终目标是获取链接列表,对吗?

这将为您提供链接:

content = urlopen('http://en.wikipedia.org/wiki/Philosophy')
soup = BeautifulSoup(content, "html.parser")
base=soup.find('div', id="bodyContent")

for link in BeautifulSoup(str(base), "html.parser").findAll("a"):
    if 'href' in link.attrs:
        print(link['href'])
Run Code Online (Sandbox Code Playgroud)