不可用的类型'切片'

时间:2016-12-02 19:53:09

标签: python python-3.x beautifulsoup

我试图编写代码来获取网站并跳过前20个字符并打印下一个字符。

这是我到目前为止的代码:

i = 0
while i <len(urls):
    html = urllib.request.urlopen(urls[i])
    bsobject = BeautifulSoup(html.read(), "html.parser")    
    print(bsobject.body[20:120])
    i+=1

我运行它并且它给了我错误&#34; TypeError:unhashable类型:&#39; slice&#39; &#34;

感谢任何帮助:)

1 个答案:

答案 0 :(得分:0)

for url in urls:
    html = urllib.request.urlopen(url)
    bsobject = BeautifulSoup(html.read(), "html.parser")
    print(str(bsobject.body)[20:121])

这段代码有效,我不确定为什么,但确实如此。

相关问题