【发布时间】:2017-11-01 12:28:38
【问题描述】:
我目前正在使用爬虫来分析数据并使用 Python 2.7、BeautifulSoup、Requests、Json 等制作网站图表...
我想用明确的关键字进行搜索,然后抓取不同商品的价格以得出平均值。
所以我尝试用 BeautifulSoup 像往常一样抓取 json 响应,但它给我的响应是:
{"data":{"uuid":"YNp-EuXHrw","index_name":"Listing","default_name":null,"query":"supreme box logo","filters":{"strata":["basic","grailed","hype"]}}}
我的请求转到:https://www.grailed.com/api/searches,这是我在搜索时在索引页面上找到的 URL。
我发现"uuid":"YNp-EuXHrw"(始终是不同的值)设置为定义将显示项目数据的 URL,如:https://www.grailed.com/feed/YNp-EuXHrw
所以我请求使用
从 api 中抓取 uuidresponse = s.post(url, headers=headers, json=payload)
res_json = json.loads(response.text)
print response
id = res_json['data']['uuid']
但问题是,当我向
发出请求时https://www.grailed.com/feed/YNp-EuXHrw
或者无论 uuid 是什么,我都会收到 <Response [500]>。
我的整个代码是:
import BeautifulSoup,requests,re,string,time,datetime,sys,json
s = requests.session()
url = "https://www.grailed.com/api/searches"
payload = {
"index_name":"Listing_production","query":"supreme box logo sweatshirts","filters":{"strata":["grailed","hype","basic"],"category_paths":[],"sizes":[],"locations":[],"designers":[],"min_price":"null","max_price":"null"}
}
headers = {
"Host": "www.grailed.com",
"Connection":"keep-alive",
"Content-Length": "217",
"Origin": "null",
"x-api-version": "application/grailed.api.v1",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
"content-type": "application/json",
"accept": "application/json",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4",
}
response = s.post(url, headers=headers, json=payload)
res_json = json.loads(response.text)
print response
id = res_json['data']['uuid']
urlID = "https://www.grailed.com/feed/" + str(id)
print urlID
response = s.get(urlID, headers=headers, json=res_json)
print response
正如您在通过 Chrome 或 URL 快速更改的任何内容执行请求时所看到的那样
大吃一惊。 com
到
grailed.com/feed/uuid
所以我尝试向这个 URL 发出 GET 请求,但只得到响应 500。
如何抓取显示在 uuid URL 上的数据,因为它甚至没有出现在网络请求中?
我希望我很清楚,对不起我的英语
【问题讨论】:
-
看起来网站是用 ReactJS 写的。这个问题和答案可以帮助stackoverflow.com/questions/29972996/how-to-parse-dom-react
-
@oshaiken 谢谢你,将在 CasperJS 上记录
-
我之前用过 PhantomJS 和 Selenium
-
@oshaiken 那么什么是最好的? CasperJS 还是 PhantomJS?我现在更喜欢避免使用 Selenium,如果这是我最后的选择,我会使用它......你还有一些代码吗? Google 上的教程非常混乱,因为它有很多 Javascript...
-
不需要javascript。
标签: python json web-scraping beautifulsoup python-requests