【问题标题】:Python requests through API with variable URL in json to scrape contentPython 通过 API 请求在 json 中具有可变 URL 以抓取内容
【发布时间】: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 中抓取 uuid
response = 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


【解决方案1】:

安装 phantomJs。 http://phantomjs.org/ 不是一个完整的解决方案,但希望这会有所帮助。 点安装硒 npm install phantomjs

test.py

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.PhantomJS(executable_path='/usr/local/bin/phantomjs') //path to phantomjs driver
driver.set_window_size(1120, 550)

driver.get("https://www.grailed.com/")

try:
    //you want to wait untill page is renderded 
    element = WebDriverWait(driver,1).until(
        EC.presence_of_all_elements_located((By.XPATH,'//*[@id="homepage"]/div/div[3]/div[1]/div/form/label/input'))
    )
    element = driver.find_element_by_xpath('//*[@id="homepage"]/div/div[3]/div[1]/div/form/label/input')

    if element.is_displayed():
        element.send_keys('search this')
    else:
        print ('no element')

except Exception as e:
    print (e)





print (driver.current_url)
driver.quit()

【讨论】:

  • 谢谢,你帮了大忙
猜你喜欢
  • 2021-12-27
  • 1970-01-01
  • 2019-07-14
  • 1970-01-01
  • 2018-03-29
  • 2015-05-12
  • 2021-09-05
  • 1970-01-01
  • 2019-04-26
相关资源
最近更新 更多