Skip to content

Instantly share code, notes, and snippets.

@wesbos
Created January 25, 2019 20:30
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save wesbos/4d05bcc6aac16866259e818de1d1c4ad to your computer and use it in GitHub Desktop.
Save wesbos/4d05bcc6aac16866259e818de1d1c4ad to your computer and use it in GitHub Desktop.
marketplace
require('isomorphic-fetch');
async function go(params) {
const variables = {
params: {
bqf: { callsite: 'COMMERCE_MKTPLACE_WWW', query: 'iphone' },
browse_request_params: {
// burlington
filter_location_id: '108043585884666',
// hamilton
// filter_location_id: '104011556303312',
filter_price_lower_bound: 0,
filter_price_upper_bound: 214748364700,
},
custom_request_params: {
surface: 'SEARCH',
search_vertical: 'C2C',
},
},
};
const res = await fetch('https://www.facebook.com/api/graphql/', {
headers: {
'content-type': 'application/x-www-form-urlencoded',
},
body: `variables=${JSON.stringify(variables)}&doc_id=2022753507811174`,
method: 'POST',
mode: 'cors',
});
const { data } = await res.json();
console.log(data);
// Pagination Cursor - doesn't seem to work if I pass it above
console.log(data.marketplace_search.feed_units.page_info);
data.marketplace_search.feed_units.edges.forEach(
({
node: {
product_item: { for_sale_item },
},
}) => {
console.log(for_sale_item.group_commerce_item_title);
}
);
}
go();
@wesbos
Copy link
Author

wesbos commented Jan 25, 2019

to run: npm install isomorphic-fetch and then node facebookScrape.js

@menjilx
Copy link

menjilx commented Jan 26, 2019

is this use a official Facebook API?

@craighooghiem
Copy link

is this use a official Facebook API?

const res = await fetch('https://www.facebook.com/api/graphql/', {

@csellis
Copy link

csellis commented Dec 2, 2019

@wesbos Has your sniper app stopped working on Facebook? Mine isn't returning the listings anymore

@wesbos
Copy link
Author

wesbos commented Dec 2, 2019

I haven't looked at it in a bit - the endpoint seems the same but maybe they need some other headers. How i would test it- open incognito window, go to marketplace, search for something, find the XHR request, copy as fetch, and then start deleting headers until it stops working

@csellis
Copy link

csellis commented Dec 2, 2019

Looks like line 26 body requirements have changed. I just dumped the body of my xhr request in there and it's working. Also the shape returned by graphql has changed. I've run out of time working on it now, so I'll just show what I've got. Not pretty.

const res = await fetch('https://www.facebook.com/api/graphql/', {
    credentials: 'include',
    headers: {
      accept: '*/*',
      'accept-language': 'en-US,en;q=0.9',
      'content-type': 'application/x-www-form-urlencoded',
      'sec-fetch-mode': 'cors',
      'sec-fetch-site': 'same-origin',
      'viewport-width': '277',
    },
    referrer:
      'https://www.facebook.com/marketplace/raleigh/search/?query=golf%20cart&vertical=C2C&sort=BEST_MATCH',
    referrerPolicy: 'origin-when-cross-origin',
    // body: `variables=${JSON.stringify(variables)}&doc_id=2022753507811174`,
    body:
      'av=0&__user=0&__a=1&__dyn=7xeUmBwjbgmwCwKKEKEW74jFwn84a2i5U4e1FxebzEdF989Euxa0z8S2S4okwAwxxicw9m7oqx61BwvU2Vwb-q3q5Voy6o2xwbG783pwKx-8wlU-cBweq0wXAy85iaxq3m7Eaoy15wJwBgK7o88vwEwf62W2C4U2IzUuxy485W1dxe5o&__csr=&__req=3&__pc=PHASED%3ADEFAULT&dpr=1&__rev=1001480872&__s=f2tba8%3A86i7l7%3Ab63no8&__hsi=6765868467750761585-0&lsd=AVpb1DF9&jazoest=2605&__spin_r=1001480872&__spin_b=trunk&__spin_t=1575301510&fb_api_caller_class=RelayModern&fb_api_req_friendly_name=MarketplaceSearchResultsPageContainerNewQuery&variables=%7B%22MARKETPLACE_FEED_ITEM_IMAGE_WIDTH%22%3A196%2C%22VERTICALS_LEAD_GEN_PHOTO_HEIGHT_WIDTH%22%3A40%2C%22MERCHANT_LOGO_SCALE%22%3Anull%2C%22params%22%3A%7B%22bqf%22%3A%7B%22callsite%22%3A%22COMMERCE_MKTPLACE_WWW%22%2C%22query%22%3A%22golf%20cart%22%7D%2C%22browse_request_params%22%3A%7B%22filter_location_id%22%3A%22raleigh%22%2C%22commerce_search_sort_by%22%3A%22BEST_MATCH%22%2C%22filter_price_lower_bound%22%3A0%2C%22filter_price_upper_bound%22%3A214748364700%7D%2C%22custom_request_params%22%3A%7B%22surface%22%3A%22SEARCH%22%2C%22search_vertical%22%3A%22C2C%22%7D%7D%7D&doc_id=3456763434364354',
    method: 'POST',
    mode: 'cors',
  });
  const { data } = await res.json();
  const { edges } = data.marketplace_search.feed_units;
  const nodes = edges.map(edge => {
    const { listing } = edge.node;
    console.log(listing);
    return {
      listing,
    };
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment