把下列代码中的数据库转换为MySQL数据库语法 for spu in spus: data = [] spuid = spu.xpath('./@data-sku')[0] data.append(spuid) link = 'https://item.jd.com/{spu}.html'.format(spu=spuid) data.append(link) price = spu.xpath('.//div[@class="gl-i-wrap"]/div[2]/strong/i/text()') if price: data.append(price[0]) else: data.append(None) # mongdb里存入的是null title = spu.xpath('.//div[@class="gl-i-wrap"]/div[3]/a/em/text()') if title: data.append(title[0]) else: data.append(None) comment_count = spu.xpath('.//div[@class="gl-i-wrap"]/div[4]/strong/a/text()') if comment_count: data.append(comment_count[0]) else: data.append(None) shop = spu.xpath('.//div[@class="gl-i-wrap"]/div[5]/span/a/text()') if shop: data.append(shop[0]) else: data.append(None) # print(data) # 存入mongodb spu_data = {'spuid': data[0], 'link': data[1], 'price': data[2], 'title': data[3], 'comment_count': data[4], 'shop': data[5]} print(spu_data) result = coll.insert_one(spu_data) print('插入成功:' + str(result.inserted_id)) temp.append(data) print(len(temp)) except selenium.common.exceptions.StaleElementReferenceException: print('parse_page: StaleElementReferenceException') browser.refresh()
时间: 2024-02-14 18:36:10 浏览: 142
抱歉,代码中的数据库类型无法直接转换为 MySQL 数据库语法。您需要重新定义 MySQL 数据库的表结构,并按照 MySQL 语法将数据存入 MySQL 数据库中。下面是一个示例,仅供参考:
```
import pymysql
# 连接 MySQL 数据库
conn = pymysql.connect(host='localhost', user='root', password='password', database='mydatabase')
cursor = conn.cursor()
for spu in spus:
data = []
spuid = spu.xpath('./@data-sku')[0]
data.append(spuid)
link = 'https://item.jd.com/{spu}.html'.format(spu=spuid)
data.append(link)
price = spu.xpath('.//div[@class="gl-i-wrap"]/div[2]/strong/i/text()')
if price:
data.append(price[0])
else:
data.append(None) # mongdb里存入的是null
title = spu.xpath('.//div[@class="gl-i-wrap"]/div[3]/a/em/text()')
if title:
data.append(title[0])
else:
data.append(None)
comment_count = spu.xpath('.//div[@class="gl-i-wrap"]/div[4]/strong/a/text()')
if comment_count:
data.append(comment_count[0])
else:
data.append(None)
shop = spu.xpath('.//div[@class="gl-i-wrap"]/div[5]/span/a/text()')
if shop:
data.append(shop[0])
else:
data.append(None)
# 定义 MySQL 数据库表结构
sql = "INSERT INTO spu (spuid, link, price, title, comment_count, shop) VALUES (%s, %s, %s, %s, %s, %s)"
# 存入 MySQL 数据库
cursor.execute(sql, tuple(data))
conn.commit()
print('插入成功:' + str(cursor.lastrowid))
cursor.close()
conn.close()
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)