1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Quora Bot Source Code

Discussion in 'General Programming Chat' started by yellowcat, Jun 22, 2017.

Tags:
  1. yellowcat

    yellowcat Regular Member

    Joined:
    Aug 27, 2015
    Messages:
    296
    Likes Received:
    163
    Location:
    internet 24/7
    Home Page:
    I was able to obtain roughly 1.5 million hits to my questions in a week., i asked roughly 1k questions Mostly just got them from scraping YA and other websites.
    Thought i'd share the source code( it's written in Python 2.7/bs4/selenium)
    [​IMG]

    Code:
    #all cats are yellow
    from selenium import webdriver
    from bs4 import BeautifulSoup
    import time
    
    
    #Quora Login Information
    email=""
    passy=""
    
    # File With Questions Here
    filey = ""
    
    #Read File ,strip new lines ,return question list
    def readFile(filey):
        with open(filey, "r") as f:
            q = f.readlines()
        qlist = [x.strip() for x in q]
        # qlist=reversed(qlist) #Will reverse the question list if needed
        print len(qlist), "Total Questions Loaded"
        return qlist
    
    #Login to Quora
    def login(email, passy):
        print "Logging in..."
        driver.get("http://quora.com")
    
        # Create Soup Object and find all form_column classes
        forms = BeautifulSoup(driver.page_source, "lxml").find_all(class_="form_column")
    
        # Iterate through forms
        # Find polymorphic id string,append a hashtag(#) to create css_selector
        for form in forms:
            try:
                # This is for email/password entry box
                data = form.find("input")["name"]
                if data == "email":
                    email_css = "#" + form.find("input")["id"]
                if data == "password":
                    password_css = "#" + form.find("input")["id"]
            except:
                pass
    
            try:
                # This is for the Login Button
                data = form.find("input")["value"]
                if data == "Login":
                    button_css = "#" + form.find("input")["id"]
            except:
                pass
    
        driver.find_element_by_css_selector(email_css).send_keys(email)
        driver.find_element_by_css_selector(password_css).send_keys(passy)
        time.sleep(2)
        driver.find_element_by_css_selector(button_css).click()
        time.sleep(2)
        # LOGIN FINISHED
    
    
    #Create Question List
    qlist = readFile(filey)
    
    #Create Webdriver Vroom Vroom
    driver = webdriver.Chrome()
    
    #Total Questions Posted Counter
    county=0
    
    # Iterate through qlist ask questions till no more
    for question in qlist:
        try:
            print question
            driver.get("http://quora.com")
            soup=BeautifulSoup(driver.page_source,"lxml")
    
            # Find all text areas
            blox = soup.find_all("textarea")
    
            # Find polymorphic id string for Ask Question entry field
            for x in blox:
                try:
                    placeholder = x["placeholder"]
                    if placeholder.__contains__("Ask or Search Quora"): # Fix this later
                        askbar_css = "#" + x["id"]
                        print askbar_css
                except:
                    pass
    
    
            askbutton = "#" + soup.find(class_="AskQuestionButton")["id"]# Fix this later
    
            # Type out Question
            driver.find_element_by_css_selector(askbar_css).send_keys(question)
    
            # Wait for askbutton to become clickable
            time.sleep(.2) # Fix later
            try:
                driver.find_element_by_css_selector(askbutton).click()
            except:
                #Click Failed # Fix later
                pass
    
            # Find the popup
            while True:
                try:
                    soup = BeautifulSoup(driver.page_source, "lxml")
                    popExists = soup.find(class_="Modal AskQuestionModal")
                    break
                except:
                    pass
            soup = BeautifulSoup(driver.page_source,"lxml")
            popup = "#" + soup.find(class_="submit_button modal_action")["id"]
            driver.find_element_by_css_selector(popup).click()
    
            for x in range(0,17):
                time.sleep(.1)
                try:
                    soup = BeautifulSoup(driver.page_source, "lxml")
                    popExists = soup.find(class_="PMsgContainer") #Found Popup
    
                    if str(popExists).__contains__("You asked"): #big no no
                        county += 1
                        break
                except:
                    pass
            print "county=>",county
    
    
        except Exception,e:
            print e
            print "ERROR"
            pass
    
    Apologies i kinda just wrote it on a whim, then went over it again changed a few things and added some notes. I'd love if anyone could critique my code :)
     
    • Thanks Thanks x 3
    Last edited: Jun 22, 2017
  2. ergoprime

    ergoprime Junior Member

    Joined:
    Nov 3, 2016
    Messages:
    105
    Likes Received:
    18
    Occupation:
    SOCIAL MEDIA MARKETING
    Thanks for the great share! By the way.... what do you gain from that? Can you promote something to your followers? Do you sell your account?

    Obviously I am not very familiar with the marketing opportunities regarding quora, except gaining traffics via links. It would be great if you elaborate further.
     
  3. yellowcat

    yellowcat Regular Member

    Joined:
    Aug 27, 2015
    Messages:
    296
    Likes Received:
    163
    Location:
    internet 24/7
    Home Page:
    I was thinking of creating questions and answers. Answering your own questions on a separate account with links. This would work really well with say "school questions/answers" or other niches. Could also use ask as anonymous to prevent bans.
    Could also be useful for growing upvote accounts
     
  4. ergoprime

    ergoprime Junior Member

    Joined:
    Nov 3, 2016
    Messages:
    105
    Likes Received:
    18
    Occupation:
    SOCIAL MEDIA MARKETING
    Great idea! Now we just need to know the spam regulations of quora (How often can you post links per day per IP without getting banned) and we could drive viral traffic to our sites.

    Thanks again!
     
  5. ergoprime

    ergoprime Junior Member

    Joined:
    Nov 3, 2016
    Messages:
    105
    Likes Received:
    18
    Occupation:
    SOCIAL MEDIA MARKETING
    But I guess that's easy to answer if you experiment a bit.
     
  6. Geasy

    Geasy Jr. VIP Jr. VIP

    Joined:
    Jul 26, 2016
    Messages:
    611
    Likes Received:
    94
    Ohh i miss ubuntu.

    Could i see ur user url jake?

    How it answered question
     
  7. living2xl

    living2xl Jr. VIP Jr. VIP

    Joined:
    Dec 9, 2011
    Messages:
    1,596
    Likes Received:
    306
    Occupation:
    Sippin dat juice - Shout it louder!
    Location:
    Not sleeping!
    Home Page:
    Can you share the YH scrape script plz
     
  8. living2xl

    living2xl Jr. VIP Jr. VIP

    Joined:
    Dec 9, 2011
    Messages:
    1,596
    Likes Received:
    306
    Occupation:
    Sippin dat juice - Shout it louder!
    Location:
    Not sleeping!
    Home Page:
    C:\selenium>C:\selenium\quora.py
    File "C:\selenium\quora.py", line 20
    print len(qlist), "Total Questions Loaded"
    ^
    SyntaxError: invalid syntax


    I use this
    # File With Questions Here
    filey = "questions1.txt"

    What am I doing wrong?
     
  9. xyzhat

    xyzhat Registered Member

    Joined:
    Apr 18, 2016
    Messages:
    59
    Likes Received:
    14
    You may be using a diferent version of Python. Try to download 2.7 version.
    Try using this line if you're using python 3

    Code:
    print(len(qlist), "Total Questions Loaded")
     
    Last edited: Jul 14, 2017
  10. Gogol

    Gogol Jr. VIP Jr. VIP

    Joined:
    Sep 10, 2010
    Messages:
    3,409
    Likes Received:
    3,057
    Gender:
    Male
    Lovely!