How do I scrape a full instagram page in python?

前端 未结 3 2029
独厮守ぢ
独厮守ぢ 2021-01-01 09:25

Long story short, I\'m trying to create an Instagram python scraper, that loads the entire page and grabs all the links to the images. I have it working, only problem is, it

相关标签:
3条回答
  • 2021-01-01 09:54

    @zero's answer is incomplete (at least as of 1/15/19). c.send_keys is not a valid method. Instead, this is what I did:

    c = webdriver.Chrome()
    c.get(some_url)
    
    element = c.find_element_by_tag_name('body') # or whatever tag you're looking to scrape from
    
    for i in range(10):
        element.send_keys(Keys.END)
        time.sleep(1)
    
    soup = BeautifulSoup(c.page_source, 'html.parser')
    
    0 讨论(0)
  • 2021-01-01 09:55

    parse_ig.py

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    from bs4 import BeautifulSoup
    from InstagramAPI import InstagramAPI
    import time
    
    c = webdriver.Chrome()
    # load IG page here, whether a hashtag or a public user's page using c.get(url)
    
    for i in range(10):
        c.send_keys(Keys.END)
        time.sleep(1)
    
    soup = BeautifulSoup(c.page_source, 'html.parser')
    ids = [a['href'].split('/') for a in soup.find_all('a') if 'tagged' in a['href']]
    

    Once you have the ids, you can use Instagram's old API to get data for those. I'm not sure if it still works, but there was an API that I used -- which was limited by how much FB has slowly deprecated parts of the old API. Here's the link, in case you don't want to access Instagram API on your own :)

    You can also add improvements to this simple code. Like instead of a "for" loop, you could do a "while" loop instead (i.e. while page is still loading, keep pressing END button.)

    0 讨论(0)
  • 2021-01-01 10:07

    As Scratch already mentioned, Instagram uses "infinite scrolling" which won't allow you to load the entire page. But you can check the total amount of messages at the top of the page (within the first span with the _fd86t class). Then you can check if the page already contains all of the messages. Otherwise, you'll have to use a GET request to get a new JSON response. The benefit to this is that this request contains the first field, which seems to allow you to modify how many messages you get. You can modify this from its standard 12 to get all of the remaining messages (hopefully).

    The necessary request looks similar to the following (where I've anonymised the actual entries, and with some help from the comments):

    https://www.instagram.com/graphql/query/?query_hash=472f257a40c653c64c666ce877d59d2b&variables={"id":"XXX","first":12,"after":"XXX"}
    
    0 讨论(0)
提交回复
热议问题