Skip to content

Instantly share code, notes, and snippets.

@viggy28
Created November 9, 2019 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viggy28/574f923dffae73d350ecdbcefa656631 to your computer and use it in GitHub Desktop.
Save viggy28/574f923dffae73d350ecdbcefa656631 to your computer and use it in GitHub Desktop.
# header file imported
from urllib import urlopen
from bs4 import BeautifulSoup
from selenium import webdriver
# give the location of chromedrive installed on your machine.
# path = r'C:\\Users\\gaurav\\Desktop\\Chromedriver'
# ref: https://www.tutorialspoint.com/python_web_scraping/python_web_scraping_dynamic_websites.htm
path=""
driver = webdriver.Chrome(executable_path = path)
# creating a file name
file = "Details.csv"
f = open(file, "w") #open the file
Headers = "Name,Location\n" #heading part
f.write(Headers) #inserting the heading
# range = [0 to 2]
# loop for multi page open
for i in range(0,3):
url = "https://stackoverflow.com/users?page=()&tab=reputation&filter=week".format(i)
driver.get(url)
soup = BeautifulSoup(driver.page_source,'html.parser')
print("soup")
# soup now list all data including tags such as python, mysql
print(soup)
# finding the name of the user inside the class file
Title = soup.find_all("div", {"class":"user-details"})
print(len(Title)) #total lenth of the user in the page
# loop for getting the name
for i in Title:
try:
name = i.find('a').get_text()
location = i.find('span', class_ ='user-location')
# print(name)
# print(locatin)
name_list = (name)
# location_list = (location)
f.write(name_list)
# f.write(location_list)
f.write("\n")
except: AttributeError
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment