簡體   English   中英

無法從網站上抓取數據

[英]Unable to scrape data from a website

我想從以下網站獲取商品價格: https : //paytm.com/shop/p/demonio-SUNDEMONIOS-R-193973BC69538C?tracker=%7C%7C%7C%7C%2Fh%2Fbrand-store% 2時尚-暢銷-最暢銷%20Products%7C1

價格位於以下標簽中:

<span ng-if="!product.product.isOnlyCarCategory">Buy for Rs 79</span>

我使用以下代碼,但是它返回一個空列表。

import requests
from bs4 import BeautifulSoup
s=str(raw_input())
r=requests.get(s)
soup=BeautifulSoup(r.content)
item_name=soup.find_all("span",{"ng-if":"!product.product.isOnlyCarCategory"})
print item_name

通過將&callback=angular.callbacks._0&channel=web&version=2附加到URL,可以獲取包含產品數據的json。

https://catalog.paytm.com/v1/p/demonio-SUNDEMONIOS-R-193973BC69538C?tracker=%7C%7C%7C%7C%2Fh%2Fbrand-store%2Ffashion-sale-Best-Selling%20Products%7C1&callback=angular.callbacks._0&channel=web&version=2

然后,您可以像這樣解析結果(假設您對價格感興趣):

import json
import requests

r = requests.get(url)
d = json.loads(r.text.split('\n')[1][:-2])
print(d['offer_price'])

以上將給您79

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM