question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Function get_posts return always 12 posts

See original GitHub issue

Hi, sorry for bothering you again. As you see in the title each time i run get_posts on a instagram profile it return always 12 posts. My code is : sessionid = '********************

    lista = []
    # headers = {"User-Agent": "user-agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Mobile Safari/537.36 Edg/87.0.664.57",
    # "cookie": f"sessionid={os.environ.get('sessionid')};"}
    headers = {
        "User-Agent": "user-agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Mobile Safari/537.36 Edg/87.0.664.57",
        "cookie": f"sessionid={sessionid};"}
    options = Options()
    options.add_argument('--headless')
    driver = webdriver.Chrome(options=options)
    target = 'https://www.instagram.com/molteni_matteo/'
    # target = 'https://www.instagram.com/lubecreopratolacasteldisangro/'
    insta_profile = Profile(target)
    insta_profile.scrape(headers=headers)
    insta_profile.url = target
    list_post = insta_profile.get_posts(webdriver=driver,amount=13)
    print(insta_profile.followers)
    print("Numero post : " + str(len(list_post)))
    for profile_post in list_post:
        profile_post.scrape(headers=headers)
        lst = get_image_urls(profile_post)
        # for y in lst:
        # print("LINK INSIDE POST : ", y)
        # html = profile_post.embed()
        # soup = BeautifulSoup(html, "html.parser")
        # href = None
        # for a in soup.find_all('a', href=True):
        # href = a['href']
        # break
        # sep = '?'
        # href = href.split(sep, 1)[0]
        # href = href+"?__a=1"
        # print('href : ', href)
        # response = requests.get(href,headers=headers).json()
        # json_data = json.loads(response.text)
        # print(response)
        # print(response.get('edge_sidecar_to_children'))
        post_dict = profile_post.to_dict(metadata=False)
        post_dict['images_links'] = lst
        lista.append(post_dict)
    for x in lista:
        print(x)
    print('fine')`

list_post = insta_profile.get_posts(webdriver=driver,amount=13) Even if i put None on amount or i don’t specify amount it gives me always 12 posts. How can i solve this problem ? And sorry to disturb you again.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
Matteoo98commented, Jan 28, 2021

Ok thanks for your help , i think the problem is going to be bigger than my ability but thank you for all 💯

1reaction
Matteoo98commented, Jan 29, 2021

Hi @chris-greening , after a lot of work i finally realized my purpose. I was able to use the get_post method to get all posts on a page and not just 12. What I did was use this extension https://chrome.google.com/webstore/detail/cookie-editor/hlkenndednhfkekhgcdicdfddnkalmdm to get the json of the cookies of a session opened on instagram where I was logged in (but could be implemented with pickle). I saved the json on the project directory and then I put the json inside the driver that uses the get_post method so it used a session where I was already authenticated. This is the code :

driver.get('https://www.instagram.com')
with open('C:\\Users\\matti\\git\\ProgettoLube\\ProgettoLube\\WebInspector\\cookie_instagram.json', 'r',newline='') as input_data:
    cookies = json.load(input_data)
for i in cookies:
driver.add_cookie(i)
list_post = insta_profile.get_posts(webdriver=driver)

At the end the method returned a complete list of all posts from an instagram profile ! What you think about ? In my opinion this could be implemented in the library to get the process all automated and without manual login. P.S. Instagram in this way instagram does not get angry or suspicious

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is the first item returned by get_posts() always the latest post?
Yes: According to https://developer.wordpress.org/reference/functions/get_posts/ the default is ordered by date in a descending order.
Read more >
get_posts() | Function - WordPress Developer Resources
Retrieves an array of the latest posts, or posts matching the given criteria.
Read more >
php - Wordpress WP_Query/get_posts where post_title equals ...
Even if I use get_posts() it will always return all snippets in the same order and will not return the single snippet based...
Read more >
WordPress get_posts: How to Use This PHP Function to Build ...
WordPress get_posts is a powerful function allowing developers to retrieve pieces of content from the WordPress database.
Read more >
Designing a class to manage WordPress posts - Carl Alexander
You need to perform another step and call get_posts . This will return the array of WP_Post objects. Once you have the array,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found