import requests
from bs4 import BeautifulSoup
 
res = requests.get('http://news.sina.com.cn/c/nd/2017-06-12/doc-ifyfzhac1650783.shtml')
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text,'html.parser')
#取评论数
commentCount = soup.select_one('#commentCount1')
print(commentCount.text)

为空,这是因为保存在js里面了

import json
comments = requests.get('http://comment5.news.sina.com.cn/page/info?version=1&format=js&channel=gn&newsid=comos-fyfzhac1650783')
comments.encoding = 'utf-8'
print(comments)
jd = json.loads(comments.text.strip('var data=')) #移除改var data=将其变为json数据
print(jd['result']['count']['total'])

移除 var data= 因为在获取时字符串前缀是包含var data=的 其不符合json数据格式 因此转化时需将其从请求内容中移除

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-31
  • 2022-12-23
  • 2021-05-27
  • 2022-02-05
  • 2021-12-20
  • 2021-11-22
猜你喜欢
  • 2022-01-27
  • 2021-12-01
  • 2021-08-06
  • 2021-09-04
  • 2021-12-04
  • 2021-12-01
  • 2021-12-14
相关资源
相似解决方案