print(icerik [0] .text)IndexError:列表索引超出范围错误

时间:2018-07-31 08:18:15

标签: python beautifulsoup urllib

from bs4 import BeautifulSoup
import urllib.request
url = "http://www.python.tc/python-nedir"
url_oku = urllib.request.urlopen(url)
soup = BeautifulSoup(url_oku, 'html.parser')
icerik = soup.find_all('div',attrs={'class':'single-post-content'})
print(icerik[0].text)

Traceback (most recent call last):
  File "C:\Users\hira\AppData\Local\Programs\Python\Python35\d.py", line 11, in <module>
    print(icerik[0].text)
IndexError: list index out of range

1 个答案:

答案 0 :(得分:0)

查看源代码,页面不包含类<div>的{​​{1}}。要获取帖子内容,请尝试获取single-post-content标签:

<article>

这将打印:

from bs4 import BeautifulSoup
import urllib.request
url = "http://www.python.tc/python-nedir"
url_oku = urllib.request.urlopen(url)
soup = BeautifulSoup(url_oku, 'html.parser')

icerik = soup.find('article',attrs={'class':'single-post'})
print(icerik.text)