无法从网站上抓取数据

时间:2016-04-30 12:51:02

标签: python angularjs web-scraping beautifulsoup

我想从这个网站获得一件商品的价格: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 个答案:

答案 0 :(得分:1)

您可以通过在网址上附加&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

相关问题