gpt4 book ai didi

python - 如何将 Python/Beautiful Soup 中的网页抓取数据存入 MySQL 数据库

转载 作者:行者123 更新时间:2023-11-29 10:26:32 25 4
gpt4 key购买 nike

虽然我在 Python 中获得了超过 10 个项目的结果,但现在我只能获得最后一个出现在我的 MySQL 数据库中的产品(ID 为 12 以及价格、图片等信息)。我需要修复它,以便它们全部出现,而不仅仅是一种产品。

Python 代码如下。

import requests
from bs4 import BeautifulSoup
import mysql.connector

url = 'https://www.newegg.com/Video-Cards-Video-Devices/Category/ID-38?Tpk=graphics%20card'

source = requests.get(url).text

soup = BeautifulSoup(source, 'lxml')



conn = mysql.connector.connect(host='127.0.0.1', user='x', database='scrape',password="x")
cursor = conn.cursor()



item_container = soup.find_all('div', class_='item-container')


def get_data():
lists = []
for index, item_name in enumerate(item_container):
name = item_name.find_all('a', class_='item-title')[0].text
lists.append({'name': name})
lists[index]['index'] = index


for index, item_price in enumerate(item_container):
price = item_price.find('li', class_='price-current').find('strong')
if price == None:

price == ('Not Available')
lists[index]['price'] = price


else:

price = ('$' + price.text +'.99')
prices = []
lists[index]['price'] = price

for index, item_picture in enumerate(item_container):
picture = 'http:' + item_picture.find('img', class_='lazy-img')['data-src']

lists[index]['picture'] = picture

for index, item_shipping in enumerate(item_container):
shipping = (item_shipping.find('li', class_='price-ship').text).strip()
lists[index]['shipping'] = shipping


def create_table():

val_index = lists[index]['index']
val_name = lists[index]['name']
val_picture = lists[index]['picture']
val_price = lists[index]['price']
val_shipping = lists[index]['shipping']


add_item = ("INSERT INTO newegg "
"(id, itemname, itempic, itemprice, itemshipping) "
"VALUES (%s, %s, %s, %s, %s)")

data_item = (val_index, val_name, val_picture, val_price, val_shipping)


cursor.execute("DELETE FROM newegg ")
conn.commit()
cursor.execute(add_item, data_item)
conn.commit()

cursor.close()
conn.close()

create_table();
get_data()

最佳答案

所以需要修复的主要问题是create_table()。我们不希望它在插入项目之前删除数据库内容。此外,我们需要循环列表中的所有项目。我会这样做。

def create_table():
cursor.execute("DELETE FROM newegg ")
conn.commit()

for product in lists:
val_index = product['index']
val_name = product['name']
val_picture = product['picture']
val_price = product['price']
val_shipping = product['shipping']


add_item = ("INSERT INTO newegg "
"(id, itemname, itempic, itemprice, itemshipping) "
"VALUES (%s, %s, %s, %s, %s)")

data_item = (val_index, val_name, val_picture, val_price, val_shipping)

cursor.execute(add_item, data_item)
conn.commit()

注意,create_table() 也不再为您关闭连接。我建议在初始化它的同一范围(在本例中为全局范围)中关闭连接。函数 create_table() 并不“拥有”连接资源,因此不应允许它销毁它。尽管初始化和销毁​​函数内部的连接是非常有意义的。

另外,请注意,每次进行抓取时,这都会清除您的表格。这可能没问题,但如果您想随着时间的推移更改您的 id,请不要从一开始就删除,并让您的 id 列自动递增或其他。

关于python - 如何将 Python/Beautiful Soup 中的网页抓取数据存入 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48190135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com