关于提取json数据
from bs4 import BeautifulSoupimport requests
from pyquery import PyQuery as pq
import json
url='https://www.zhihu.com/question/404413681/answer/1354281798'
headers={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'
}
html=requests.get(url,headers=headers).text
soup=BeautifulSoup(html,'lxml')
doc=pq(html)
items=doc('#js-initialData').items()
for item in items:
print(item.text())
运行后得到一长串数据,怎么提取出其中的汉字内容尼? 可以用正则提取汉字:
from bs4 import BeautifulSoup
import requests
from pyquery import PyQuery as pq
import json
import re
url = 'https://www.zhihu.com/question/404413681/answer/1354281798'
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'
}
html = requests.get(url, headers=headers).text
soup = BeautifulSoup(html, 'lxml')
doc = pq(html)
items = doc('#js-initialData').items()
for item in items:
print(re.findall(r'[\u4e00-\u9fa5]+', item.text())) zltzlt 发表于 2020-7-30 10:17
可以用正则提取汉字:
不能用key索引,提取值吗? zltzlt 发表于 2020-7-30 10:17
可以用正则提取汉字:
我要提取里面的content的value zltzlt 发表于 2020-7-30 10:17
可以用正则提取汉字:
from bs4 import BeautifulSoup
import requests
from pyquery import PyQuery as pq
import json
import re
url='https://www.zhihu.com/question/404413681/answer/1354281798'
headers={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'
}
html=requests.get(url,headers=headers).text
soup=BeautifulSoup(html,'lxml')
doc=pq(html)
items=doc('#js-initialData').items()
for item in items:
print(re.findall(r'"content":"(.*?)"',item.text()))
就算用正则提取出了content的value,怎么样把他按下图的格式打印出来尼,还有每行文字都有些符号怎么去掉尼
页:
[1]