尝试使用 BS4 从 Trustpilot 抓取日期时,出现以下 JSON 错误 - Python

问题描述

我正在使用以下脚本从 Trustpilot 网站抓取用户评论数据,以使用我希望抓取的 https://ca.trustpilot.com/review/www.hellofresh.ca 中的数据对用户情绪进行一些分析

日期、星级、评论内容。

但是当我运行代码时,出现以下错误,谁能帮忙解释一下原因?

JSONDecodeError:期望值:第 1 行第 1 列(字符 0)

stars = []
dates = []
comments = []
results = []
with requests.Session() as s:
    for num in range(1,2):
        url = "https://ca.trustpilot.com/review/www.hellofresh.ca?page={}".format(num)
        r = s.get(url,headers = headers)
        soup = BeautifulSoup(r.content,'lxml')

        for star in soup.find_all("section",{"class":"review__content"}):

            # Get rating value
            rating = star.find("div",{"class":"star-rating star-rating--medium"}).find('img').get('alt')

            # Get date value
            #date_json = json.loads(star.find('script').text)
            #date = date_json['publishedDate']
            
            date_tag = star.select("div.review-content-header__dates > script")    
            date = json.loads(date_tag[0].text)
            dt = datetime.strptime(date['publishedDate'],"%Y-%m-%dT%H:%M:%SZ")
            
            
            # Get comment
            comment = star.find("div",class_="review-content__body").text

            stars.append(rating)
            dates.append(dt)
            comments.append(comment)

            data = {"Rating": rating,"Review": comment,"Dates": date}
            results.append(data)

        time.sleep(2)


print(results)```


解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)