我正在尝试加载在特定站点的站点地图上找到的所有链接,然后加载所有这些链接并获取更多数据(库存,尺寸及其ID)。到目前为止,代码已找到所有链接,并将它们转换为.json,但是当它加载每个网站并抓取更多数据时,它只会将其链接到站点地图中的最后一个链接。我需要它来完成站点地图中的所有链接。如果有人可以帮助我,那就太好了!
谢谢:)
def check_endpoint():
url = 'https://shopnicekicks.com/sitemap_products_1.xml'
page = requests.get(url)
soup = BeautifulSoup(page.text,'lxml')
for url in soup.find_all('loc'): #load the url and find all product links.
produrl = url.text
UrlDB.append(produrl)
endpoint = produrl + '.json' #take a product links and convert to .json
JsonUrl = endpoint
#load each product link and find variants.
req = requests.get(JsonUrl)
reqJson = json.loads(req.text)
CartLink = JsonUrl.split("/")[2]
CartLink = "https://{}".format(CartLink)
for product in reqJson['product']['variants']:
Variants = product['id']
Size = product['title']
Price = product['price']
Stock = product['inventory_quantity']
atclink = "Size = {}, Stock = {}, Link = {}, /cart/{}:1 ".format(Size, Stock, CartLink, Variants)
print (atclink) #print all variants
return
答案 0 :(得分:0)
在示例中间看到该注释了吗?缩进该注释及其后的所有空格4个空格。