我有一个经销商,我想使用scrape方法将他的所有产品添加到我的网站,因为他没有xml文件。我复制了之前用于其他网站的相同代码,但效果不佳。
我的策略是找到一个类别中的所有产品链接,然后单独找到所有产品链接中的所有产品属性,并将它们添加到woocommerce中的数据库中。我将对所有类别重复此过程。
我获取和插入数据的代码如下所示;
include ("../wp-load.php");
include 'simple_html_dom.php';
include 'function.php';
$url = $_POST['url'];
$kategori1 = $_POST['kategori1'];
$kategori2 = $_POST['kategori2'];
$kategori3 = $_POST['kategori3'];
$opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0\r\n"));
$context = stream_context_create($opts);
$html = file_get_html($url,false,$context);
$count=0;
foreach($html->find('div[id=urunresim] a') as $element){
$product = file_get_html('http://isiksilver.com/'.$element->href,false,$context);
foreach($product->find('span[class=yazilar]',1) as $kod);
foreach($product->find('span[class=yazilar]',3) as $gram);
foreach($product->find('img',53) as $image);
$thumb = 'http://isiksilver.com/'.$image->src;
$thumb_name = basename($thumb);
$fiyat = str_replace(" GR", "", $gram)*8.5/1.8;
$my_post = array();
$my_post['post_title'] = $kod->innertext;
$my_post['post_content'] = '<table>'.$kod.' Ateş Kehribarı Tesbih</table>';
$my_post['post_status'] = 'publish';
$my_post['post_author'] = 1;
$my_post['post_type'] = 'product';
// Yazıyı veritabanına ekle
$the_post_id = wp_insert_post( $my_post );
$upload_dir = wp_upload_dir();
$image_data = file_get_contents($thumb);
$filename = $thumb_name;
if(wp_mkdir_p($upload_dir['path']))
$file = $upload_dir['path'] . '/' . $filename;
else
$file = $upload_dir['basedir'] . '/' . $filename;
file_put_contents($file, $image_data);
$wp_filetype = wp_check_filetype($filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', $filename ),
'post_content' => '',
'post_status' => 'inherit',
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename )
);
$attach_id = wp_insert_attachment( $attachment, $file, $the_post_id );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $the_post_id, $attach_id );
add_post_meta( $the_post_id, 'total_sales', '0' );
add_post_meta( $the_post_id, '_downloadable', 'no' );
add_post_meta( $the_post_id, '_virtual', 'no' );
add_post_meta( $the_post_id, '_regular_price', $fiyat );
add_post_meta( $the_post_id, '_sale_price', $fiyat );
add_post_meta( $the_post_id, '_base_currency_price', $fiyat );
add_post_meta( $the_post_id, '_base_currency', 'TL' );
add_post_meta( $the_post_id, '_tax_status', 'taxable' );
add_post_meta( $the_post_id, '_tax_class', '18' );
add_post_meta( $the_post_id, '_visibility', 'visible' );
add_post_meta( $the_post_id, '_purchase_note', '' );
add_post_meta( $the_post_id, '_weight', '' );
add_post_meta( $the_post_id, '_length', '' );
add_post_meta( $the_post_id, '_height', '' );
add_post_meta( $the_post_id, '_width', '' );
add_post_meta( $the_post_id, '_sku', $kod );
add_post_meta( $the_post_id, '_variant_description', '' );
add_post_meta( $the_post_id, '_product_attributes', 'a:0:{}' );
add_post_meta( $the_post_id, '_sale_prices_dates_from', '' );
add_post_meta( $the_post_id, '_sale_prices_dates_to', '' );
add_post_meta( $the_post_id, '_price', $fiyat );
add_post_meta( $the_post_id, '_sold_individually', 'yes' );
add_post_meta( $the_post_id, '_manage_stock', 'yes' );
add_post_meta( $the_post_id, '_backorders', 'no' );
add_post_meta( $the_post_id, '_stock_status', 'instock' );
add_post_meta( $the_post_id, '_stock', '5' );
add_post_meta( $the_post_id, '_thumbnail_id', $attach_id );
add_post_meta( $the_post_id, '_featured', 'no' );
wp_set_object_terms( $the_post_id, $brand->plaintext, 'yith_product_brand' );
wp_set_object_terms( $the_post_id, (int)$kategori1, 'product_cat', true );
wp_set_object_terms( $the_post_id, (int)$kategori2, 'product_cat', true );
wp_set_object_terms( $the_post_id, (int)$kategori3, 'product_cat', true );
$count++;
}
echo $count.' ürün eklendi<br>';
它将所有产品添加到具有相同名称的db(并且值与应该的值不同)。我可以通过以下代码从单个产品页面获得正确的价值;
$product->find('span[class=yazilar]',1);
$product->find('span[class=yazilar]',3);
但它无法在foreach中获得正确的值(我尝试删除foreach并且它获得了正确的值,但它只将第一个产品添加到db中,并且这次不添加元数据)。
我该如何解决这个问题?