productDict[prodArticle].update({attrName: attrValue})
productDict[prodArticle] = {
attrName: attrValue
}
import csv
import unicodecsv as csv
import requests
from bs4 import BeautifulSoup
import os, sys
requests.packages.urllib3.disable_warnings()
PATH = os.path.abspath(os.path.dirname(sys.argv[0])) + '\\'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
subCatUrl = "https://viatec.ua/catalog/pribori-nichnogo-bachennia"
productDict = dict()
response = requests.get(subCatUrl, headers=headers, verify=False)
html = response.text
soup = BeautifulSoup(html, "html.parser")
checkBoxUrls = soup.find_all("input", class_="filter-main__element-checkbox")
checkBoxUrlsList = []
for checkBoxUrl in checkBoxUrls:
checkBoxUrl = checkBoxUrl.get("data-href")
if checkBoxUrl != subCatUrl + "/0:0":
checkBoxUrlsList.append(checkBoxUrl)
productDict = dict()
for checkBoxUrl in checkBoxUrlsList:
response = requests.get(checkBoxUrl, headers=headers, verify=False)
html = response.text
soup = BeautifulSoup(html, "html.parser")
attrName = soup.find("span", class_="filter__label").text
attrValue = soup.find("span", class_="filter__delete-text").text
prodItemUrlsList = []
if soup.find_all("a", class_="paggination__page"):
paggNums = soup.find_all("a", class_="paggination__page")
paggPageList = []
for paggNum in paggNums:
paggNum = paggNum.text
paggPageList.append(checkBoxUrl + ";page:" + paggNum)
for paggPage in paggPageList:
response = requests.get(paggPage, headers=headers, verify=False)
html = response.text
soup = BeautifulSoup(html, "html.parser")
prodArticles = soup.find_all("p", class_="categories__item-code")
for prodArticle in prodArticles:
prodArticle = prodArticle.text
productDict[prodArticle][attrName] = attrValue
else:
prodArticles = soup.find_all("p", class_="categories__item-code")
for prodArticle in prodArticles:
prodArticle = prodArticle.text
productDict[prodArticle][attrName] = attrValue
print(productDict)
global $product;
$my_var = wc_get_product($product);
$product_cat_id = $my_var->get_category_ids()[0];
if ( $product_cat_id == 16 ) {
$size_tab_link = '<a target="_blank" href="ссылка1">текст1</a>;
}
if ( $product_cat_id == 17 ) {
$size_tab_link = '<a target="_blank" href="ссылка2">текст2</a>;
}
и т.д.
а потом просто вывод:
<div class="size-grid-product"><?php echo $size_tab_link ?></div>
Теперь оно прекрасно добавляет несуществующее пары, и на ключ изначально не ругается потому что мы делаем проверку на наличие этого ключа в словаре, если его нет - создаём его, если есть - обновляем его значения.