2016-04-30 75 views
0

我想從這個網站得到一個項目的價格抽取數據:https://paytm.com/shop/p/demonio-SUNDEMONIOS-R-193973BC69538C?tracker=%7C%7C%7C%7C%2Fh%2Fbrand-store%2Ffashion-sale-Best-Selling%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 

回答

1

通過在URL中附加&callback=angular.callbacks._0&channel=web&version=2,您可以獲得包含產品數據的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