Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 引发JSONDecodeError(“;期望值”;,s,err.value)_Python_Json_Selenium_Beautifulsoup - Fatal编程技术网

Python 引发JSONDecodeError(“;期望值”;,s,err.value)

Python 引发JSONDecodeError(“;期望值”;,s,err.value),python,json,selenium,beautifulsoup,Python,Json,Selenium,Beautifulsoup,我正在尝试抓取数据,但是代码抛出了一个关于json.loads的错误。当我追溯到错误时,我意识到循环中的元素是None,因此json.loads无法运行 有什么解决办法吗 下面是我的代码: import json from selenium import webdriver import pandas as pd from bs4 import BeautifulSoup from datetime import datetime start_time = datetime.now() d

我正在尝试抓取数据,但是代码抛出了一个关于
json.loads
的错误。当我追溯到错误时,我意识到循环中的元素是
None
,因此
json.loads
无法运行

有什么解决办法吗

下面是我的代码:

import json
from selenium import webdriver
import pandas as pd
from bs4 import BeautifulSoup
from datetime import datetime

start_time = datetime.now()


data = []
 
op = webdriver.ChromeOptions()
op.add_argument('--ignore-certificate-errors')
op.add_argument('--incognito')
op.add_argument('--headless')
driver = webdriver.Chrome(executable_path='D:/Desktop/Query/chromedriver.exe',options=op)
driver.get('https://www.cdiscount.com/f-1175520-MIS2008813786478.html')
link = 'https://www.cdiscount.com/f-1175520-MIS2008813786478.html'
soup = BeautifulSoup(driver.page_source, 'html.parser')
b = soup.prettify()
product_title = soup.find('title').getText()
reviews = soup.find_all("script",type="application/ld+json")
for element in reviews : 
     json_string = element.getText()
     json_dict = json.loads(json_string)
     data.append(json_dict)

您可以通过访问元素的
内容来读取JSON

for element in reviews: 
     json_string = ' '.join(element.contents)
     json_dict = json.loads(json_string)
     data.append(json_dict)
关于
getText
的美丽群组:

如果只希望在文档或标记中包含人类可读的文本,则 可以使用get_text()方法

从Beauty Soup版本4.9.0开始,当使用lxml或html.parser时 不支持使用、内容和标记 被认为是“文本”,因为这些标记不是 人体可见的pag含量*


这就是为什么在您的示例中,
getText
返回一个空字符串,需要使用
contents

您可以通过访问元素的
contents
来读取JSON

for element in reviews: 
     json_string = ' '.join(element.contents)
     json_dict = json.loads(json_string)
     data.append(json_dict)
关于
getText
的美丽群组:

如果只希望在文档或标记中包含人类可读的文本,则 可以使用get_text()方法

从Beauty Soup版本4.9.0开始,当使用lxml或html.parser时 不支持使用、内容和标记 被认为是“文本”,因为这些标记不是 人体可见的pag含量*

这就是为什么在您的示例中,
getText
返回一个空字符串,并且需要使用
contents