【发布时间】:2020-05-04 18:37:34
【问题描述】:
我正在尝试从此处脚本标签中的代码中提取campaign_hearts 和postal_code(整个代码太长,无法发布):
<script>
...
"campaign_hearts":4817,"social_share_total":11242,"social_share_last_update":"2020-01-17T10:51:22-06:00","location":{"city":"Los Angeles, CA","country":"US","postal_code":"90012"},"is_partner":false,"partner":{},"is_team":true,"team":{"name":"Team STEVENS NATION","team_pic_url":"https://d2g8igdw686xgo.cloudfront.net
...
我可以使用以下代码识别我需要的脚本:
from bs4 import BeautifulSoup
import numpy as np
import pandas as pd
from time import sleep
import requests
import re
import json
page = requests.get("https://www.gofundme.com/f/eric-stevens-care-trust")
soup = BeautifulSoup(page.content, 'html.parser')
all_scripts = soup.find_all('script')
all_scripts[0]
但是,我不知道如何提取我想要的值。 (我对 Python 很陌生。) This thread 为类似问题推荐了以下解决方案(经过编辑以反映我正在使用的 html)。
data = json.loads(all_scripts[0].get_text()[27:])
但是,运行它会产生错误:JSONDecodeError: Expecting value: line 1 column 1 (char 0).
既然我已经识别了正确的脚本,我可以做些什么来提取我需要的值?我也尝试过here 列出的解决方案,但在导入 Parser 时遇到了问题。
【问题讨论】:
标签: python html json python-3.x beautifulsoup