2016-09-22 40 views
0

我试图构建一个爬虫,我想打印 我使用Python 3.5Python的类型错误回溯(最近通话最后一个)

在该网页上的所有链接有我的代码

import requests 
from bs4 import BeautifulSoup 
def crawler(link): 
    source_code = requests.get(link) 
    source_code_string = str(source_code) 
    source_code_soup = BeautifulSoup(source_code_string,'lxml') 
    for item in source_code_soup.findAll("a"): 
     title = item.string 
     print(title) 

crawler("https://www.youtube.com/watch?v=pLHejmLB16o") 

但我得到这样

TypeError         Traceback (most recent call last) 
<ipython-input-13-9aa10c5a03ef> in <module>() 
----> 1 crawler('http://archive.is/DPG9M') 

TypeError: 'module' object is not callable 
+0

收到这个错误你尝试过重命名你的'crawler'方法? –

+0

是的,我把“crawler”改成了“cat”,但是还是一样的错误 – Travis

回答

2

如果意图是只打印链接的标题,你是一个小失误的错误,更换线路:

source_code_string = str(source_code) 

使用

source_code_string = source_code.text 

除此之外代码看起来罚款和运行。 让我们称之为web_crawler_v1.py

import requests 
from bs4 import BeautifulSoup 
def crawler(link): 
    source_code = requests.get(link) 
    source_code_string = source_code.text 
    source_code_soup = BeautifulSoup(source_code_string,'lxml') 
    for item in source_code_soup.findAll("a"): 
     title = item.string 
     print(title) 


crawler("https://www.youtube.com/watch?v=pLHejmLB16o") 

文件以及有关错误,你不应该,如果你正确地调用文件这样

python3 wen_crawler_v1.py 
+0

谢谢它的作品! – Travis

+0

我还有一个问题。我正在使用Jupyter笔记本,并且当我在笔记本RootPython中键入这些代码时,它就可以工作。但是当我创建一个新的py文件(test.py)并在其中放入相同的代码并键入“import test.py”时,它不起作用,你知道为什么吗? – Travis

+0

我已经测试过它,它可以正常工作,但它是否在您从中调用它的同一位置?因为如果不是,那可能会成为问题。 –

相关问题