使用beautifulsoup提取内容

使用beautifulsoup提取丁香园网页内容
Mac电脑打开chrome浏览器,打开要抓的网页,检查:
使用beautifulsoup提取内容
上代码:
import urllib.request
from bs4 import BeautifulSoup as bs
def main():
headers = {
“User-Agent”: “User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3717.0 Safari/537.36”
}
url = ‘http://www.dxy.cn/bbs/thread/626626
request = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(request).read().decode(“utf-8”)
html = bs(response, ‘lxml’)
getItem(html)
def getItem(html):
datas = []
for data in html.find_all(“tbody”):
try:
userid = data.find(“div”, class_=“auth”).text
print(userid)
content = data.find(“td”, class_=“postbody”).text
print(content)
datas.append((userid,content))
except:
pass
print(datas)

if name == ‘main’:
main()
代码显示有问题,上代码截图:
使用beautifulsoup提取内容

Mac终端,输出结果:
使用beautifulsoup提取内容