简体   繁体   English

无法从网站上抓取数据

[英]Unable to scrape data from a website

I want to get the price of a item from this website : https://paytm.com/shop/p/demonio-SUNDEMONIOS-R-193973BC69538C?tracker=%7C%7C%7C%7C%2Fh%2Fbrand-store%2Ffashion-sale-Best-Selling%20Products%7C1 我想从以下网站获取商品价格: https : //paytm.com/shop/p/demonio-SUNDEMONIOS-R-193973BC69538C?tracker=%7C%7C%7C%7C%2Fh%2Fbrand-store% 2时尚-畅销-最畅销%20Products%7C1

The price is located in the following tag: 价格位于以下标签中:

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

I use the following code , but it returns an empty list. 我使用以下代码,但是它返回一个空列表。

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

You can get the json that contains the product data, by appending &callback=angular.callbacks._0&channel=web&version=2 to the URL. 通过将&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

You can then parse the result like this (I assume you are interested in the price): 然后,您可以像这样解析结果(假设您对价格感兴趣):

import json
import requests

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

The above will give you 79 . 以上将给您79

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM