- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
虽然我在 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/
我不知道“汤”字面意思在与计算机图形相关的“三角形汤”或“多边形汤”中使用时是什么意思。是不是和我们用勺子吃饭的“汤”有关? (我的母语不是英语。) 最佳答案 维基百科来拯救! A polygon s
我们正在废弃 Amazon.in 网站以检索任何产品的价格。所有产品在“span”标签中的“id”属性都具有不同的值,例如; id = 'priceblock_ourprice', id = 'p
我有一个这样的模板: 和这样的输入 HTML COMPLEX HTML 其中 COMPLEX_HTML 是很多子标签(很干净 - 验证) 我试图将输入 HTML 的 body 标记内的 HTML
我对 soup('tag_name') 和 soup.find_all('tag_name') 之间的区别感到困惑。下面是一个包含一小段 html 的示例: from bs4 import Beaut
我正在尝试使用 css 选择器解析 html 页面 import requests import webbrowser from bs4 import BeautifulSoup page = req
这是网页 HTML 源代码的一部分: apple banana cherry melon 我想提取我想要的网址,比如以/Result 开头的网址?我刚刚了解到您可以在 beautiful soup
我注意到一个非常烦人的错误:BeautifulSoup4(包:bs4)经常发现比以前版本(包:BeautifulSoup)更少的标签。 这是该问题的一个可重现的实例: import requests
所以我一直在试图弄清楚如何抓取一个购买/销售网站的网站,我发现了 HTML 中的所有内容,但该类包含不同的随机数,例如:
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwa
我正在尝试加载 html 页面并输出文本,即使我正确获取网页,BeautifulSoup 以某种方式破坏了编码。 来源: # -*- coding: utf-8 -*- import requests
题目地址:https://leetcode.com/problems/soup-servings/description/ 题目描述: There are two types of soup: t
您好,我正在尝试从网站获取一些信息。请原谅我,如果我的格式有任何错误,这是我第一次发布到 SO。 soup.find('div', {"class":"stars"}) 从这里我收到 我需要 “
我想从 Google Arts & Culture 检索信息使用 BeautifulSoup。我检查了许多 stackoverflow 帖子( [1] , [2] , [3] , [4] , [5]
我决定学习 Python,因为我现在有更多时间(由于大流行)并且一直在自学 Python。 我试图从一个网站上刮取税率,几乎可以获得我需要的一切。下面是来自我的 Soup 变量以及相关 Python
我正在使用 beautifulsoup 从页面中获取所有链接。我的代码是: import requests from bs4 import BeautifulSoup url = 'http://ww
我正在尝试根据部分属性值来识别 html 文档中的标签。 例如,如果我有一个 Beautifulsoup 对象: import bs4 as BeautifulSoup r = requests.ge
Показать телефон 如何在 Beautiful Soup 中找到上述元素? 我尝试了以下方法,但没有奏效: show = soup.find('div', {'class': 'acti
我如何获得结果网址:https://www.sec.gov/Archives/edgar/data/1633917/000163391718000094/0001633917-18-000094-in
我是 python 新手,尝试从页面中提取表格,但无法使用 BS4 找到该表格。你能告诉我我哪里出错了吗? import requests from bs4 import BeautifulSoup
我有一个巨大的 XML 文件(1.2 G),其中包含数百万个 MusicAlbums 的信息,每个 MusicAlbums 都具有如下简单格式 P 22 Exitos
我是一名优秀的程序员,十分优秀!