In [1]:
import requests  # to make GET request
from bs4 import BeautifulSoup  # to parse the HTML response
import time  # to pause between calls
import pandas as pd  # to see CSV
import os
import re

os.chdir('../data/')

In [2]:
columns=['post id','title','text','href','user id','mother post id']
df = pd.DataFrame(columns=columns)

columns=['user id','user description']
df_users = pd.DataFrame(columns=columns)

# Initialize post index
post_id=0

In [3]:
def add_post(post_id,title,text,url,mother_post_id,user_id,user_name):
    global df,df_users
    #
    # Add post data to dataframe
    #
    newrow={"post id":post_id,
            "title":title,
            "text":text,
            "href":url,
            "user id":user_id,
            "mother post id":mother_post_id}
    df.loc[len(df.values)]=newrow
    # Update user dataframe:
    #
    newrow={"user id":user_id,
          "user description":user_name}
    if user_id not in df_users['user id'].values:
        df_users.loc[len(df_users)]=newrow
    
    
#url="http://ehealthforum.com/health/autism-recovery-success-story-t351300.html"
def parse_post(url):
    global df,df_users,post_id

    user_href_prefix="http://ehealthforum.com/health/user_profile_"
    p = re.compile('\n+')

    response = requests.get(url)
    page_source = response.text
    soup = BeautifulSoup(page_source, 'html5lib')
    mother_post_id=post_id
    post_id=post_id+1

    #
    # Extract data from soup:
    #
    post = soup.find("div", class_="vt_h2")
    title=post.find("h1", class_="caps").text
    #
    # Mother post:
    post1=soup.find("div", class_="vt_first_message_body")
    #
    #
    # User
    #
    user_data = soup.find("span",class_="vt_asked_by_user")
    user_name = user_data.text
    user_id = user_data.find("a")['href'].replace(user_href_prefix,"").replace(".html","")
    user_description = soup.find("span",class_="vt_user_rank").text
    user_name=user_name+" "+user_description    
    #
    # Post text
    #
    text=post1.text.replace("\t","")
    text=p.sub('\n', text)
    #
    # Add post to dataframe
    #
    add_post(post_id,title,text,url,mother_post_id,user_id,user_name)
    #
    # Follow up to that message
    #messages=soup.find_all("div", class_="vt_message_body")
    #for message in messages:
    #    post_id=post_id+1
    #    text=message.text.replace("\t","")
    #    text=p.sub('\n', text)
    postrows=soup.find_all("div",class_="vt_postrow_rest")
    for postrow in postrows[:len(postrows)-1]:
        post_id=post_id+1
        text=postrow.find("div",class_="vt_post_body").text
        text=text.replace("\t","")
        text=p.sub('\n', text)
        user_id=postrow.find("a")['href'].replace(user_href_prefix,"").replace(".html","")
        user_name=postrow.find("div",class_="vt_asked_by_user").text
        user_description=postrow.find("span",class_="vt_user_rank").text
        user_name=user_name+" "+user_description
        add_post(post_id,title,text,url,mother_post_id,user_id,user_name)
for message in messages: post_id=post_id+1 #title=message.find("div",title="subject").text mm=message.text.replace("\t","") mm=p.sub('\n', mm)
url="http://ehealthforum.com/health/autism-recovery-success-story-t351300.html" parse_post(url) df

In [4]:
df_users


Out[4]:
user id user description

In [5]:
url_list=["http://ehealthforum.com/health/autism.html",
          "http://ehealthforum.com/health/autism_medical_questions_242_0_50.html",
          "http://ehealthforum.com/health/autism_medical_questions_242_0_100.html",
          "http://ehealthforum.com/health/autism_medical_questions_242_0_150.html",
          "http://ehealthforum.com/health/autism_medical_questions_242_0_200.html"]

for url in url_list:
    response = requests.get(url)
    page_source = response.text
    soup = BeautifulSoup(page_source, 'html5lib')

    content=soup.find_all("div",class_="fp_topic_content_title")

    for topic in content:
        url=topic.find("a", class_='topictitle')['href']
        print(url)
        parse_post(url)


http://ehealthforum.com/health/possible-autism-signs-t230405.html
http://ehealthforum.com/health/autism-i-think-he-is-on-point-for-yrs-old-t312115.html
http://ehealthforum.com/health/symptoms-of-autism-t200703.html
http://ehealthforum.com/health/autism-recovery-success-story-t351300.html
http://ehealthforum.com/health/increased-intracranial-pressure-associated-with-autism-t512431.html
http://ehealthforum.com/health/absent-mindedness-forgetfulness-unable-to-retain-information-t509071.html
http://ehealthforum.com/health/anyone-here-not-use-the-word-disability-t507272.html
http://ehealthforum.com/health/autism-i-have-year-old-twin-girls-recently-diagnosed-t503978.html
http://ehealthforum.com/health/asperger-s-mild-autism-physical-disability-eligible-for-ssi-t420171.html
http://ehealthforum.com/health/am-i-autistic-t414569.html
http://ehealthforum.com/health/autism-in-children-during-pregnancy-t409556.html
http://ehealthforum.com/health/autism-and-schizoaffective-disorder-t204315.html
http://ehealthforum.com/health/autism-how-to-get-my-child-to-sleep-on-his-own-t441473.html
http://ehealthforum.com/health/benefits-of-autism-schools-to-support-your-child-s-education-t445799.html
http://ehealthforum.com/health/can-aspergers-develop-to-autism-t427595.html
http://ehealthforum.com/health/hfa-high-functioning-autism-t395381.html
http://ehealthforum.com/health/months-autism-t409257.html
http://ehealthforum.com/health/does-he-have-autism-questioning-validity-of-the-diagnosis-t428848.html
http://ehealthforum.com/health/symptoms-of-autism-t429210.html
http://ehealthforum.com/health/autism-what-is-it-t391138.html
http://ehealthforum.com/health/yeard-old-with-autism-getting-worse-t443828.html
http://ehealthforum.com/health/a-new-discovery-on-this-autism-awareness-month-t452464.html
http://ehealthforum.com/health/how-to-prove-i-don-t-have-autism-t475064.html
http://ehealthforum.com/health/is-it-possible-that-my-kids-got-autism-from-me-t420957.html
http://ehealthforum.com/health/can-autism-be-grown-out-of-t380021.html
http://ehealthforum.com/health/how-autism-affects-the-growth-of-the-child-t446012.html
http://ehealthforum.com/health/topic116210.html
http://ehealthforum.com/health/test-that-identify-such-things-as-autism-memory-prob-t450826.html
http://ehealthforum.com/health/autism-and-possible-ticks-t474839.html
http://ehealthforum.com/health/i-have-autism-t367024.html
http://ehealthforum.com/health/autism-t503789.html
http://ehealthforum.com/health/possibility-of-autism-t483769.html
http://ehealthforum.com/health/topic121686.html
http://ehealthforum.com/health/autism-t494894.html
http://ehealthforum.com/health/self-harm-im-autistic-and-i-hit-myself-out-of-frustration-t492419.html
http://ehealthforum.com/health/no-interaction-with-anyone-strange-behaviour-t444534.html
http://ehealthforum.com/health/adult-stumbling-on-words-t368279.html
http://ehealthforum.com/health/mos-old-spinning-doesn-t-talk-autism-or-spd-t461365.html
http://ehealthforum.com/health/i-need-help-will-it-get-better-am-i-the-only-one-t443024.html
http://ehealthforum.com/health/topic115729.html
http://ehealthforum.com/health/aspergers-diagnosis-t295966.html
http://ehealthforum.com/health/what-does-adhd-and-bipolar-mean-t429935.html
http://ehealthforum.com/health/future-purposes-the-chances-of-the-kids-having-autism-t438945.html
http://ehealthforum.com/health/is-asperger-s-hereditary-t174875.html
http://ehealthforum.com/health/why-do-some-dentists-still-use-papoose-boards-t441984.html
http://ehealthforum.com/health/alternate-medicines-for-mental-development-t381657.html
http://ehealthforum.com/health/jesus-is-the-healer-and-can-heal-all-diseases-t163435.html
http://ehealthforum.com/health/is-my-son-autistic-t427486.html
http://ehealthforum.com/health/the-relationship-between-imagery-dyslexia-hyperlexia-and-autis-t197717.html
http://ehealthforum.com/health/mild-pdd-yr-old-screaming-during-ot-t413400.html
http://ehealthforum.com/health/aspergers-syndrome-t406845.html
http://ehealthforum.com/health/aspergers-or-is-she-just-unfriendly-for-some-reason-t418606.html
http://ehealthforum.com/health/my-husband-has-asperger-s-and-i-am-scared-to-become-pregnant-t380102.html
http://ehealthforum.com/health/toddler-almost-possible-stimming-t366348.html
http://ehealthforum.com/health/aspergers-syndrome-with-signs-of-tourettes-syndrome-t361760.html
http://ehealthforum.com/health/aspergers-and-i-ve-really-started-to-wonder-t377217.html
http://ehealthforum.com/health/is-it-likely-that-i-have-aspergers-t416414.html
http://ehealthforum.com/health/chances-of-child-with-as-when-both-parents-have-family-history-t411954.html
http://ehealthforum.com/health/can-t-potty-train-my-almost-year-old-autistic-son-t408155.html
http://ehealthforum.com/health/do-any-of-your-autistic-children-present-these-t314741.html
http://ehealthforum.com/health/what-to-expect-in-the-long-run-yr-old-recently-diagnosed-t374764.html
http://ehealthforum.com/health/year-old-not-talking-a-single-word-very-noise-sensitive-t345506.html
http://ehealthforum.com/health/what-are-good-job-interview-questions-for-an-autism-expert-t398302.html
http://ehealthforum.com/health/asperger-syndrome-and-social-cues-t161421.html
http://ehealthforum.com/health/aspergers-behaviour-control-how-much-do-you-ignore-t393155.html
http://ehealthforum.com/health/i-think-my-year-old-son-has-autism-t376343.html
http://ehealthforum.com/health/autistic-teenager-yet-smart-o-o-t385020.html
http://ehealthforum.com/health/try-a-weight-compression-vest-for-calming-child-t346504.html
http://ehealthforum.com/health/commitment-issues-autism-depression-i-need-help-t267244.html
http://ehealthforum.com/health/normal-mri-but-still-no-speech-at-and-years-of-age-t381608.html
http://ehealthforum.com/health/does-my-brother-have-asperger-s-t379338.html
http://ehealthforum.com/health/what-type-of-professional-should-we-seek-t344775.html
http://ehealthforum.com/health/autism-and-violence-t136496.html
http://ehealthforum.com/health/coping-with-autistic-brother-t213287.html
http://ehealthforum.com/health/autistic-success-story-t197975.html
http://ehealthforum.com/health/autism-and-prozac-t285659.html
http://ehealthforum.com/health/your-thoughts-on-me-having-autism-t360317.html
http://ehealthforum.com/health/autisim-hard-time-making-friends-getting-a-job-writing-t363429.html
http://ehealthforum.com/health/topic114542.html
http://ehealthforum.com/health/am-i-autistic-i-often-have-difficulty-t361773.html
http://ehealthforum.com/health/a-possible-last-resort-if-i-am-forced-onto-workfare-t345191.html
http://ehealthforum.com/health/autistic-son-pulls-on-penis-t352806.html
http://ehealthforum.com/health/adhd-combined-and-pdd-nos-t137895.html
http://ehealthforum.com/health/could-y-o-my-son-be-autistic-t350624.html
http://ehealthforum.com/health/above-years-and-still-cannot-speak-a-complete-sentence-t290862.html
http://ehealthforum.com/health/asperger-symptoms-t170889.html
http://ehealthforum.com/health/seven-dangerous-mistakes-when-dealing-with-childhood-autism-t345170.html
http://ehealthforum.com/health/autism-pineal-gland-cyst-pericardial-effusion-t345021.html
http://ehealthforum.com/health/am-i-autistic-diagnosis-t317024.html
http://ehealthforum.com/health/rocking-violent-fits-of-rage-screaming-and-throwing-things-t344376.html
http://ehealthforum.com/health/rare-genetic-disorder-unknown-t337985.html
http://ehealthforum.com/health/very-strange-behavior-t293808.html
http://ehealthforum.com/health/i-think-i-could-have-an-autism-t336751.html
http://ehealthforum.com/health/autism-therapy-what-interest-your-kids-to-keep-going-t329584.html
http://ehealthforum.com/health/multi-fascited-problem-no-diagnosis-to-show-disability-t329523.html
http://ehealthforum.com/health/starving-themselves-is-this-a-part-of-autism-t323845.html
http://ehealthforum.com/health/possible-asperger-s-diagnosis-t323479.html
http://ehealthforum.com/health/he-sees-blue-and-pink-dots-and-they-move-t314896.html
http://ehealthforum.com/health/cure-for-autism-and-epilepsy-t325992.html
http://ehealthforum.com/health/benadryl-use-on-autistic-child-t323768.html
http://ehealthforum.com/health/what-if-he-has-autism-t323192.html
http://ehealthforum.com/health/could-my-son-s-behaviour-be-autism-t322306.html
http://ehealthforum.com/health/traditional-chinese-medicine-and-aspergers-t137892.html
http://ehealthforum.com/health/turrets-disorder-info-t227234.html
http://ehealthforum.com/health/asbergers-and-school-t146768.html
http://ehealthforum.com/health/topic101835.html
http://ehealthforum.com/health/link-between-low-levels-of-glutathione-and-autism-t313271.html
http://ehealthforum.com/health/topic1835.html
http://ehealthforum.com/health/autism-how-much-do-they-understand-t314050.html
http://ehealthforum.com/health/what-cause-uncontrollable-throat-clearing-and-how-treat-it-t310713.html
http://ehealthforum.com/health/topic109617.html
http://ehealthforum.com/health/aspergers-genetic-t146827.html
http://ehealthforum.com/health/self-employment-want-an-advocate-who-can-interpret-t308079.html
http://ehealthforum.com/health/new-evidence-please-read-t307889.html
http://ehealthforum.com/health/advocacy-t306488.html
http://ehealthforum.com/health/play-music-for-them-t302898.html
http://ehealthforum.com/health/aitism-service-dogs-t301907.html
http://ehealthforum.com/health/diagnosed-with-aspergers-t137112.html
http://ehealthforum.com/health/autism-phenomena-t282907.html
http://ehealthforum.com/health/autism-or-no-teaching-or-talking-about-things-in-general-t295006.html
http://ehealthforum.com/health/red-vision-t268867.html
http://ehealthforum.com/health/could-my-year-old-daughter-have-autism-t264733.html
http://ehealthforum.com/health/i-m-worried-i-might-be-autistic-t205426.html
http://ehealthforum.com/health/topic116657.html
http://ehealthforum.com/health/topic122668.html
http://ehealthforum.com/health/aspergers-and-bipolar-t282111.html
http://ehealthforum.com/health/fatigue-and-facial-tingling-t284684.html
http://ehealthforum.com/health/vote-for-the-wpi-to-get-funds-to-study-diseases-including-autism-t283057.html
http://ehealthforum.com/health/florinization-of-water-linked-to-autism-in-children-t281107.html
http://ehealthforum.com/health/topic124235.html
http://ehealthforum.com/health/hyperlexia-and-mild-autism-t278682.html
http://ehealthforum.com/health/gf-cf-diet-does-it-help-autisim-t275586.html
http://ehealthforum.com/health/uncertain-where-to-post-this-inquery-t272418.html
http://ehealthforum.com/health/avoiding-eye-contact-with-strangers-t270707.html
http://ehealthforum.com/health/advice-on-how-to-give-an-autistic-boy-medicine-t268045.html
http://ehealthforum.com/health/bach-flowers-autism-asperger-s-t266801.html
http://ehealthforum.com/health/speech-at-t207543.html
http://ehealthforum.com/health/should-i-look-for-a-second-oppinion-t265904.html
http://ehealthforum.com/health/topic21599.html
http://ehealthforum.com/health/topic93991.html
http://ehealthforum.com/health/possible-signs-of-aspergers-syndrome-t263524.html
http://ehealthforum.com/health/yr-old-w-autism-trouble-with-bowel-movements-t253180.html
http://ehealthforum.com/health/autism-patient-t261567.html
http://ehealthforum.com/health/aspergers-syndrome-and-depression-t260244.html
http://ehealthforum.com/health/asperger-s-support-for-husbands-and-wives-t258398.html
http://ehealthforum.com/health/description-of-autism-t143644.html
http://ehealthforum.com/health/williams-syndrome-autism-t256475.html
http://ehealthforum.com/health/dla-and-asperger-s-syndrome-t254224.html
http://ehealthforum.com/health/deprivation-of-liberty-order-t246388.html
http://ehealthforum.com/health/undiagnosed-asperger-s-t249458.html
http://ehealthforum.com/health/son-injuring-himself-t218655.html
http://ehealthforum.com/health/how-do-i-discipline-an-autistic-pre-schooler-t248984.html
http://ehealthforum.com/health/risperdone-medication-t155887.html
http://ehealthforum.com/health/designing-for-the-autistic-children-t200500.html
http://ehealthforum.com/health/early-signs-of-autism-t183793.html
http://ehealthforum.com/health/aggressive-behavior-and-autism-t204797.html
http://ehealthforum.com/health/slow-development-t204922.html
http://ehealthforum.com/health/autism-and-possible-post-traumatic-stress-t236726.html
http://ehealthforum.com/health/schizoaffective-disorder-t158285.html
http://ehealthforum.com/health/great-product-for-children-who-suffer-from-autism-t175712.html
http://ehealthforum.com/health/as-and-fear-of-sports-t141611.html
http://ehealthforum.com/health/autism-and-gastrointestinal-problems-t143252.html
http://ehealthforum.com/health/switching-schools-with-aspergers-t192019.html
http://ehealthforum.com/health/topic104655.html
http://ehealthforum.com/health/topic122167.html
http://ehealthforum.com/health/applied-behavior-analysis-t196296.html
http://ehealthforum.com/health/yr-old-lines-up-toys-and-side-gaze-t188270.html
http://ehealthforum.com/health/could-my-little-brother-have-aspergers-syndrome-t190862.html
http://ehealthforum.com/health/autistic-t173564.html
http://ehealthforum.com/health/autism-documentary-t189534.html
http://ehealthforum.com/health/autism-divorce-and-medical-treatments-t189502.html
http://ehealthforum.com/health/possible-autism-symptoms-t178998.html
http://ehealthforum.com/health/topic116047.html
http://ehealthforum.com/health/high-functioning-autistic-t177352.html
http://ehealthforum.com/health/topic116658.html
http://ehealthforum.com/health/naturopathic-medicine-t168270.html
http://ehealthforum.com/health/cooking-for-autistic-children-t162850.html
http://ehealthforum.com/health/possible-autism-t157051.html
http://ehealthforum.com/health/topic123590.html
http://ehealthforum.com/health/risperdone-and-blood-work-t157922.html
http://ehealthforum.com/health/topic116647.html
http://ehealthforum.com/health/synaesthesia-or-high-functioning-autism-t150102.html
http://ehealthforum.com/health/topic124462.html
http://ehealthforum.com/health/topic121484.html
http://ehealthforum.com/health/topic101101.html
http://ehealthforum.com/health/immune-deficiency-treatments-t137588.html
http://ehealthforum.com/health/aspergers-symptoms-t136499.html
http://ehealthforum.com/health/clinical-studies-you-can-participate-in-t137735.html
http://ehealthforum.com/health/frequent-vocal-and-body-ticks-t133587.html
http://ehealthforum.com/health/autism-spectrum-t131485.html
http://ehealthforum.com/health/topic129538.html
http://ehealthforum.com/health/topic129840.html
http://ehealthforum.com/health/topic129176.html
http://ehealthforum.com/health/topic127935.html
http://ehealthforum.com/health/topic127868.html
http://ehealthforum.com/health/topic108012.html
http://ehealthforum.com/health/topic122699.html
http://ehealthforum.com/health/topic124506.html
http://ehealthforum.com/health/topic124388.html
http://ehealthforum.com/health/topic123585.html
http://ehealthforum.com/health/topic123528.html
http://ehealthforum.com/health/topic112021.html
http://ehealthforum.com/health/topic116288.html
http://ehealthforum.com/health/topic117332.html
http://ehealthforum.com/health/screening-for-autism-t133824.html
http://ehealthforum.com/health/topic114939.html
http://ehealthforum.com/health/topic114605.html
http://ehealthforum.com/health/topic116958.html
http://ehealthforum.com/health/topic108889.html
http://ehealthforum.com/health/topic104662.html
http://ehealthforum.com/health/topic102418.html
http://ehealthforum.com/health/topic103410.html
http://ehealthforum.com/health/topic101523.html
http://ehealthforum.com/health/topic97623.html
http://ehealthforum.com/health/topic97373.html
http://ehealthforum.com/health/topic96375.html
http://ehealthforum.com/health/topic62632.html
http://ehealthforum.com/health/topic34950.html
http://ehealthforum.com/health/topic26237.html
http://ehealthforum.com/health/topic14419.html

In [6]:
df.to_csv('ehealthforum-posts.csv',index=False)
df_users.to_csv('ehealthforum-users.csv',index=False)

In [7]:
df.tail()


Out[7]:
post id title text href user id mother post id
1113 1114 New Member; Atypical Child With Atypical Ent S... \nFirstly, i'm sorry to hear of you, your fami... http://ehealthforum.com/health/topic34950.html 36562 1112
1114 1115 New Member; Atypical Child With Atypical Ent S... \nThanks for your reply. It turned out that, ... http://ehealthforum.com/health/topic34950.html 36415 1112
1115 1116 New Member; Atypical Child With Atypical Ent S... \nGlad to hear this has been remedied.\nYes, I... http://ehealthforum.com/health/topic34950.html 36562 1112
1116 1117 Autism & Hematoma \nMy nephew was recently diagnosed with autism... http://ehealthforum.com/health/topic26237.html 25306 1116
1117 1118 Autism Regression And Posibly Seizures \nIn an otherwise stable autistic boy 13 years... http://ehealthforum.com/health/topic14419.html 16327 1117