我已经尝试了以下内容:
import urllib
link = 'https://automatetheboringstuff.com/chapter7/'
f = urllib.request.urlopen(link)
myfile = f.read()
print(myfile)
,但这似乎只是返回页面的源而不是文本内容。
如果您只想获取章节文字,我认为是您的选择。
在您的情况下:
import requests
from bs4 import BeautifulSoup
res = requests.get('https://automatetheboringstuff.com/chapter7/')
soup = BeautifulSoup(res.text, 'html.parser')
print(soup.find('div', { "class" : "book" }).text)