Python 3 Selenium Script to Scrape Pinterest Profile Info of Username and Save it in CSV File

Python 3 Selenium Script to Scrape Pinterest Profile Info of Username and Save it in CSV File

Hello Everyone, In this tutorial, we are going to learn Python 3 Selenium Script to Scrape Pinterest Profile Info of Username and Save it in CSV File.

It is very easy and simple to follow these steps with me. You need Python and PyCharm IDE on your PC (or you can use one of your IDEs).

requirement.txt

selenium==3.141.0
fake-headers==1.0.2
webdriver_manager==3.2.2

pip install -r requirement.txt

pinterest.py`

Python 3 Selenium Script to Scrape Pinterest Profile Info of Username and Save it in CSV File

try:
    import argparse
    from fake_headers import Headers
    import requests
    import csv
    import json
except ModuleNotFoundError:
    print("Please download dependencies from requirement.txt")
except Exception as ex:
    print(ex)


class Pinterest:
    '''This class scraps pinterest and returns a dict containing all user data'''
    @staticmethod
    def _generate_url(username):
        return "https://pinterest.com/resource/UserResource/get/?source_url=%25{}%2F&data=%7B%22options%22%3A%7B%22field_set_key%22%3A%22profile%22%2C%22username%22%3A%22{}%22%2C%22is_mobile_fork%22%3Atrue%7D%2C%22context%22%3A%7B%7D%7D&_=1640428319046".format(username, username)

    @staticmethod
    def _make_request(url):
        headers = Headers().generate()
        response = requests.get(url, headers=headers)
        return response

    @staticmethod
    def scrap(username):
        try:

            try:
                url = Pinterest._generate_url(username)
                response = Pinterest._make_request(url)
                if response.status_code == 200:
                    response = response.json()
                else:
                    print("Failed to get Data!")
                    exit()
            except Exception as ex:
                print("Error", ex)
                exit()

            json_data = response
            data = json_data['resource_response']['data']
            print(data['pin_count'])
            f = open('info.csv', 'w')
            headerrow= ['Pins','Bio','Website','Followers','Following']
            writer = csv.writer(f)
            writer.writerow(headerrow)
            writer.writerow([data['pin_count'],data['about'],data['domain_url'],data['follower_count'],data['following_count']])
            return json.dumps(data)
        except Exception as ex:
            print(ex)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("username", help="username to search")

    args = parser.parse_args()
    print(Pinterest.scrap(args.username))

# last updated - 8th July,2022

python pinterest.py codinggeeksg

Conclusion

Here we clearly understood the Python 3 Selenium Script to Scrape Pinterest Profile Info of Username and Save it in CSV File.

Related Posts

Salesforce Tutorial

1 thought on “Python 3 Selenium Script to Scrape Pinterest Profile Info of Username and Save it in CSV File”

Comments are closed.