Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 奇怪的BeautifulSoup.findAll错误:未在函数内工作_Python_Web Scraping_Beautifulsoup_Python Requests - Fatal编程技术网

Python 奇怪的BeautifulSoup.findAll错误:未在函数内工作

Python 奇怪的BeautifulSoup.findAll错误:未在函数内工作,python,web-scraping,beautifulsoup,python-requests,Python,Web Scraping,Beautifulsoup,Python Requests,我正在尝试构建一个非常简单的刮板,作为爬虫项目的一部分来获取链接。我已经设置了以下功能来执行刮片: import requests as rq from bs4 import BeautifulSoup def getHomepageLinks(page): homepageLinks = [] response = rq.get(page) text = response.text soup = BeautifulSoup(text) for a i

我正在尝试构建一个非常简单的刮板,作为爬虫项目的一部分来获取链接。我已经设置了以下功能来执行刮片:

import requests as rq 
from bs4 import BeautifulSoup

def getHomepageLinks(page):
    homepageLinks = []
    response = rq.get(page)
    text = response.text
    soup = BeautifulSoup(text)
    for a in soup.findAll('a'):
        homepageLinks.append(a['href'])
    return homepageLinks
我将此文件保存为“scraper2.py”。尝试运行代码时,出现以下错误:

>>> import scraper2 as sc
>>> sc.getHomepageLinks('http://washingtonpost.com')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "scraper2.py", line 9, in getHomepageLinks
    for a in soup.findAll('a'):
TypeError: 'NoneType' object is not callable

如果我正确读取了错误消息,那么soup.findAll就会出现问题,但只有当findAll是函数的一部分时才会出现问题。我确信我拼写正确(不是findall或findall,这里的许多错误都是这样),并且我已经尝试了一个修复方法,使用上一篇文章中建议的lxml,但没有修复它。有人有什么想法吗?

尝试用以下内容替换for循环:

for a in soup.findAll('a'):
    url = a.get("href")
    if url != None:
        homepageLinks.append(url)

作为一项健全性检查,
print(soup.findAll)
就在导致错误的行之前。它是否打印
None
?如果是这样的话,<代码>打印(汤)<代码> >代码>打印(类型(汤))< /代码>打印?尝试使用<代码>汤。一些旧代码的痕迹,我没有完全删除发布。正在编辑。手动打印(键入(soup))时返回“”。将其作为函数运行会在打印前抛出错误。我尝试了“全部查找”,但没有任何更改。据我所知,它们是可以互换的。
for a in soup.findAll('a'):
    url = a.get("href")
    if url != None:
        homepageLinks.append(url)