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
答案 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)