鱼C论坛

 找回密码
 立即注册
查看: 1717|回复: 4

关于提取json数据

[复制链接]
发表于 2020-7-30 10:11:21 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. from bs4 import BeautifulSoup
  2. import requests
  3. from pyquery import PyQuery as pq
  4. import json

  5. url='https://www.zhihu.com/question/404413681/answer/1354281798'
  6. headers={
  7. 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
  8.               '(KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'
  9. }
  10. html=requests.get(url,headers=headers).text
  11. soup=BeautifulSoup(html,'lxml')

  12. doc=pq(html)
  13. items=doc('#js-initialData').items()
  14. for item in items:
  15.     print(item.text())
复制代码

运行后得到一长串数据,怎么提取出其中的汉字内容尼?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-7-30 10:17:08 | 显示全部楼层
可以用正则提取汉字

  1. from bs4 import BeautifulSoup
  2. import requests
  3. from pyquery import PyQuery as pq
  4. import json
  5. import re

  6. url = 'https://www.zhihu.com/question/404413681/answer/1354281798'
  7. headers = {
  8.     'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
  9.                   '(KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'
  10. }
  11. html = requests.get(url, headers=headers).text
  12. soup = BeautifulSoup(html, 'lxml')

  13. doc = pq(html)
  14. items = doc('#js-initialData').items()
  15. for item in items:
  16.     print(re.findall(r'[\u4e00-\u9fa5]+', item.text()))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-30 11:21:43 From FishC Mobile | 显示全部楼层
zltzlt 发表于 2020-7-30 10:17
可以用正则提取汉字:

不能用key索引,提取值吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-30 11:34:30 From FishC Mobile | 显示全部楼层
zltzlt 发表于 2020-7-30 10:17
可以用正则提取汉字:

我要提取里面的content的value
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-30 11:46:12 | 显示全部楼层
zltzlt 发表于 2020-7-30 10:17
可以用正则提取汉字:
  1. from bs4 import BeautifulSoup
  2. import requests
  3. from pyquery import PyQuery as pq
  4. import json
  5. import re

  6. url='https://www.zhihu.com/question/404413681/answer/1354281798'
  7. headers={
  8. 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
  9.               '(KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'
  10. }
  11. html=requests.get(url,headers=headers).text
  12. soup=BeautifulSoup(html,'lxml')

  13. doc=pq(html)
  14. items=doc('#js-initialData').items()
  15. for item in items:
  16.     print(re.findall(r'"content":"(.*?)"',item.text()))
复制代码

就算用正则提取出了content的value,怎么样把他按下图的格式打印出来尼,还有每行文字都有些符号怎么去掉尼
5.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-24 12:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表