Search code examples
pythonajaxweb-scrapingscrapyxmlhttprequest

Python, extract XHR response data from website


I am trying to extract some data from https://www.barchart.com/stocks/signals/top-bottom/top?viewName=main.

I am able to extract data from normal html using the xpath method, however i noticed that this website gets its data from a network.

I have found the location of where the data I want is (the table from the barchart website) which is shown in the picture below.

Picture of XHR response

How can i scrape just the response portion?

Thanks!


Solution

  • Try this code.

    from urllib import urlencode
    
    headers = {
        'cookie': 'XSRF-TOKEN=eyJpdiI6Ikk4cFdwcGlqMVVPRHM4MFc5Vk1ROWc9PSIsInZhbHVlIjoiUHRHbjZwSTZFTUkrTFRNZVJXczZsV2xZZnN1WEhmM1puakN5WWFsM0RUODhJRUZJYVA3XC9GZ1QyOUR5R0VqaXRmdDJIR0wyV2lBdXlFcTlxXC9HWFVqZz09IiwibWFjIjoiNTlkODY2MGM0YzJlZDQxMTI5ZmIwMmE0NWI5YzkzY2Q4NDg3MjhiODg4NDAxN2Q5NmYzYTE3MmUxZWQzZjk4MiJ9; laravel_session=eyJpdiI6ImFob1k3NWw1U2pBcWhKU3JLdEpDQkE9PSIsInZhbHVlIjoidnNNcXpKSHRmQkYyMGp2NTF4eUhTVVwvSmtidnAwMDV5eGdUWWVhZ2syTUlXaVdQV1JEUldYcG5lXC9mdUtnRkl2akNMR2ZKMkY2NFlWYTFOM2NPRm1uQT09IiwibWFjIjoiYTJhODkwMTJjZjI3NzJiOTE1YWY5MWJkYTNiYTNlMjZjNjI1YjgzZmJlYzhkMTU4Mjk1OWQ5MWEzMGU5OTM5NSJ9; _ga=GA1.2.674295962.1536391451; _gid=GA1.2.1388999250.1536391451; _gat_UA-2009749-51=1; Markets09122018PageView=1; Markets09122018WebinarClosed=true',
        'dnt': '1',
        'x-xsrf-token': 'eyJpdiI6Ikk4cFdwcGlqMVVPRHM4MFc5Vk1ROWc9PSIsInZhbHVlIjoiUHRHbjZwSTZFTUkrTFRNZVJXczZsV2xZZnN1WEhmM1puakN5WWFsM0RUODhJRUZJYVA3XC9GZ1QyOUR5R0VqaXRmdDJIR0wyV2lBdXlFcTlxXC9HWFVqZz09IiwibWFjIjoiNTlkODY2MGM0YzJlZDQxMTI5ZmIwMmE0NWI5YzkzY2Q4NDg3MjhiODg4NDAxN2Q5NmYzYTE3MmUxZWQzZjk4MiJ9',
        'accept-language': 'en-US,en;q=0.9',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36',
        'accept': 'application/json',
        'referer': 'https://www.barchart.com/stocks/signals/top-bottom/top?viewName=main',
        'authority': 'www.barchart.com',
        'accept-encoding': 'gzip, deflate, br',
    }
    
    params = (
        'lists': 'stocks.signals.top.current.us',
        'orderDir': 'asc',
        'fields': 'symbol,symbolName,lastPrice,priceChange,percentChange,opinion,opinionPrevious,opinionLastWeek,opinionLastMonth,symbolCode,symbolType,hasOptions',
        'meta': 'field.shortName,field.type,field.description',
        'hasOptions': 'true',
        'page': '1',
        'limit': '100',
        'raw': '1',
    )
    
    yield Request('https://www.barchart.com/proxies/core-api/v1/quotes/get?' + urlencode(params) , headers=headers)