繁体   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