Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Python 3 Selenium Automation Script to Download Twitter User Profile Picture Using Username in Command Line

Posted on December 21, 2022

Welcome folks today in this blog post we will be using the selenium library to download the profile picture of the twitter user using the requests module in python. All the full source code of the application will be given below.

 

 

Get Started

 

 

In order to get started you need to install the below library using the pip command as shown below

 

 

pip install selenium

 

 

pip install chromedriver_manager

 

 

After that you need to make an app.py file and copy paste the following code

 

 

app.py

 

 

Python
1
2
3
4
5
from selenium import webdriver
 
import requests
 
from webdriver_manager.chrome import ChromeDriverManager

 

 

As you can see we are importing all the libraries which are needed for this project. Now we need to install the chrome driver for selenium using the webdriver_manager module

 

 

Python
1
driver = webdriver.Chrome(ChromeDriverManager().install())

 

 

As you can see we are installing the chrome driver automatically by using the install() method. You can even pass a specific version of your chrome browser as the argument.

 

 

Python
1
2
3
4
5
6
7
username = "ronitroy"
 
# navigate to the Twitter profile page
driver.get(f'https://twitter.com/{username}/photo')
 
# wait for the page to load
driver.implicitly_wait(10)

 

 

As you can see we are providing the twitter username and then we are using the selenium get() method to go to that url and then we are waiting for 10 milliseconds just for the page to be fully loaded.

 

 

Python
1
2
3
4
5
6
7
8
# find the profile image element
profile_image = driver.find_element_by_class_name('css-9pa8cd')
 
# get the profile image URL
profile_image_url = profile_image.get_attribute('src')
 
# download the profile image
response = requests.get(profile_image_url)

 

 

As you can see we are using the selenium library to find the element by their class name and then we are getting the src attribute of that image to the client. And now can use the get() method to go to that url.

 

 

Python
1
2
3
4
5
6
# save the image to a file
with open(f'{username}.jpg', 'wb') as f:
    f.write(response.content)
 
# close the webdriver
driver.quit()

 

 

As you can see lastly we are saving the binary data into an actual image jpeg file which will be created in the same directory. And then we are closing the selenium driver.

 

 

 

 

 

Full Source Code

 

 

Wrapping the blog post this is the full source code of the application here

 

 

app.py

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from selenium import webdriver
import requests
 
from webdriver_manager.chrome import ChromeDriverManager
 
driver = webdriver.Chrome(ChromeDriverManager().install())
 
username = "ronitroy"
 
# navigate to the Twitter profile page
driver.get(f'https://twitter.com/{username}/photo')
 
# wait for the page to load
driver.implicitly_wait(10)
 
# find the profile image element
profile_image = driver.find_element_by_class_name('css-9pa8cd')
 
# get the profile image URL
profile_image_url = profile_image.get_attribute('src')
 
# download the profile image
response = requests.get(profile_image_url)
 
# save the image to a file
with open(f'{username}.jpg', 'wb') as f:
    f.write(response.content)
 
# close the webdriver
driver.quit()

Recent Posts

  • Android Java Tutorial to Change Styles & Visibility of System Bars (Top, Action & Status) Full Example
  • Android Java Project to Display & Play Audio Files From Storage inside ListView Using MediaPlayer Class
  • Android Java Project to Build MP4 Video to MP3 Audio Converter Using MediaMuxer Class
  • Android Java Project to Merge Multiple PDF Documents From Gallery Using iTextPDF Library
  • Android Java Project to Detect System Hardware & System CPU Info & Display inside TextView Widget
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme