【发布时间】:2021-03-05 00:18:58
【问题描述】:
我正在尝试爬取数据,但是代码会引发 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)
【问题讨论】:
标签: python json selenium beautifulsoup