我已将我的代码复制到准备好的语句中(我希望足够正确),现在我能够将XML中的值插入到我的SQL表中。问题是它只插入第一个产品的数据。我希望声明能够为所有产品执行此操作。
以下是我尝试插入的在线XML的布局:
<?php
$conn = new mysqli($servername, $username, $password, $dbname);
$xml=simplexml_load_file("URL-to-XML") or die("Error: Cannot create object");
foreach ($xml->programs->program->products->product->product_info->price as $price);
foreach ($xml->programs->program->products->product->product_info->price_shipping as $price_shipping);
// prepare and bind
$stmt = $conn->prepare("INSERT INTO BBB (price, price_shipping) VALUES (?,?)");
$stmt->bind_param("ss", $input_price, $input_price_shipping);
// set parameters and execute
$input_price = $price;
$input_price_shipping = $price_shipping;
$stmt->execute();
echo "New records created successfully";
$stmt->close();
$conn->close();
?>
我目前正在使用的代码,只插入第一个产品的值是:
from urllib.request import urlopen
from bs4 import BeautifulSoup
class MetaDataReader:
@staticmethod
def get_url_metadata(external_sites_url):
external_sites_html = urlopen(external_sites_url).read()
soup = BeautifulSoup(external_sites_html, "html.parser")
title = soup.title.text
image = ""
description = ""
for meta in soup.findAll("meta"):
title = MetaDataReader.get_meta_property(meta, "og:title", title)
image = MetaDataReader.get_meta_property(meta, "og:image", image)
description = MetaDataReader.get_meta_property(meta, "og:description", description)
return {'title': title, 'image': image, 'description': description}
@staticmethod
def get_meta_property(meta, property_name, default_value=""):
if 'property' in meta.attrs and meta.attrs['property'] == property_name:
return meta.attrs['content']
return default_value
我已尝试将其复制到准备好的代码中,因此,如果有关于如何改进我的代码的更多信息,请告诉我。