from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.chrome.options import Options
import re
url = input("Enter the URL you want to scrape phone number (only searches in bio:\n")
options = Options()
options.set_headless(headless=True)
options.add_argument('--disable-extensions')
options.add_argument('--hide-scrollbars')
options.add_argument('--disable-gpu')
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(options=options)
print("Opening URL .... \n")
driver.get(url)
print("sleep for 3 seconds (or implement document ready for better results)\n")
time.sleep(3)
description = driver.find_elements_by_xpath('//*[@id="react-root"]/section/main/div/header/section/div[2]')[0].get_attribute("innerText")
m = re.findall('3\d{9}', description)[0]
driver.quit()
print("Scraped Phone number is: ")
print(m)
AFAIK, no.Is it possible with selenium not to use the chrome or firefox web driver but insted to use the real chrome or firefox app?
It is possible to load custom browser profiles, if this it what you are asking. But it is still using the webdriver behind the scenes for automation.Is it possible with selenium not to use the chrome or firefox web driver but insted to use the real chrome or firefox app?
PyAutoGui is only used with Selenium and Beautifulsoup. Pyautogui currently don't have any function that will give you the source code. But if you just need the source and you are stuck with PyAutoGui then you could try implementing something like right mouse click > Save as html but that's an ugly way of doing it. My suggestion will be using selenium with bs4.@Blank_Hossain If we use PyAutoGui with chrome for example, what would be easiest way to get source code of html page?
As for tutorials, I would recommend the youtube channel "Programming with Mosh". He his some premium stuffs, but most of his videos are free and open for all.
As for scripts, start with selenium. pyautogui would be a bit stiff for you right now, I guess (but do give it a try).
Here, let me provide you a template to get started with selenium (find_elements_by_xpath is the answer to your question "how to select an element"):
It's a crappy attempt to scrape phone number from an IG profile, but it should be good enough for a start..
Code:from selenium import webdriver from selenium.webdriver.common.keys import Keys import time from selenium.webdriver.chrome.options import Options import re url = input("Enter the URL you want to scrape phone number (only searches in bio:\n") options = Options() options.set_headless(headless=True) options.add_argument('--disable-extensions') options.add_argument('--hide-scrollbars') options.add_argument('--disable-gpu') options.add_experimental_option('excludeSwitches', ['enable-logging']) driver = webdriver.Chrome(options=options) print("Opening URL .... \n") driver.get(url) print("sleep for 3 seconds (or implement document ready for better results)\n") time.sleep(3) description = driver.find_elements_by_xpath('//*[@id="react-root"]/section/main/div/header/section/div[2]')[0].get_attribute("innerText") m = re.findall('3\d{9}', description)[0] driver.quit() print("Scraped Phone number is: ") print(m)
By the way, installing python; selenium and the web drivers (chromdriver/firefoxdriver) is 60% of the battle. Once you have everything setup properly, python is like English language.
Another mistake that many newbies (including me when I started) make is not installing python via venv. Doing it via venv will make sure that you can use both python 2.x and 3.x at the same time, instead of having to install them in two separate machines.
Oh, and editors... I suggest using VS Code or pycharm for the editor.
Chrome driver in my example. But same for FF as well.Thanks for this. So lets say i navigate to the website, I would need to use firefox driver and enter login details ? correct ?