我正在尝试构建一个爬虫,我想打印该页面上的所有链接 我正在使用Python 3.5
这是我的代码
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
如果您只想打印链接的标题,那么您犯了一个小错误,请替换该行:
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
而不是
source_code = requests.get(link)
用途:
source_code = requests.get(link, verify = False)
您将收到 HTTPS 警告,但代码将执行
TypeError Traceback(最近一次调用最后一次) 单元格 In[15],第 28 行 23 df.dropna(就地=真) 25 # One-hot 编码分类特征 26 克拉 = 列转换器( 27变形金刚=[ ---> 28 ('ohe', OneHotEncoder(sparse=False), ['Cluster_Label'])]) 29 df_ohe = ct.fit_transform(df) 31 # 特征缩放
TypeError:OneHotEncoder。init()得到了意外的关键字参数“稀疏”