Exemplo n.º 1
0
def test_short_drug_name():
    """
    Tests that the get_url function does not search for drug names shorter than 4 characters
    """
    scraper = DrugRatingzScraper()
    url = scraper.get_url('ACE')
    assert not url
Exemplo n.º 2
0
def test_get_url_real_drug_name():
    """
    Tests that the get_url function returns the correct url for a standard drug name ('actos')
    """
    scraper = DrugRatingzScraper()
    url = scraper.get_url('actos')
    assert url == 'https://www.drugratingz.com/reviews/340/Drug-Actos.html'
Exemplo n.º 3
0
def test_drug_name_with_space():
    """
    Tests that the get_url function returns the correct url for a drug name
    that includes a space
    """
    scraper = DrugRatingzScraper()
    url = scraper.get_url('Ortho Tri-Cyclen Lo ')
    assert url == 'https://www.drugratingz.com/reviews/163/Drug-Ortho-Tri-Cyclen-Lo.html'
Exemplo n.º 4
0
def test_url_fake_drug_name():
    """
    Tests tha the get_url function returns 'None' for a drug name that does not have a review
    page
    """
    scraper = DrugRatingzScraper()
    url = scraper.get_url('garbage')
    assert not url
Exemplo n.º 5
0
def test_scrape_no_reviews():
    """
    Tests that the scrape function works for page with no reviews
    """
    scraper = DrugRatingzScraper()
    scraper.scrape(
        'https://www.drugratingz.com/reviews/18589/Drug-Adderall.html')
    assert len(scraper.reviews) == 0
Exemplo n.º 6
0
def test_scrape_invalid_url_no_title():
    """
    Tests that when the scrape function is called on a url that lacks a title
    (invalid url), it raises an AttributeError and returns 'None'
    """
    scraper = DrugRatingzScraper()
    returned = scraper.scrape(
        'https://www.drugratingz.com/reviews/258/Drug.html')
    assert not returned
Exemplo n.º 7
0
def test_scrape_assert_title_error():
    """
    Tests that when the scrape function is called with an invalid url that does have a
    title, but the title is wrong (doesn't have the phrase 'drug reviews') that an AssertionError
    is raised and the function returns 0
    """
    scraper = DrugRatingzScraper()
    returned = scraper.scrape('https://www.drugratingz.com/ShowThingCats.jsp')
    assert returned == 0
Exemplo n.º 8
0
def test_no_reviews():
    """
    Tests that the scrape page function returns 'None' when no reviews are
    found on the page
    """
    scraper = DrugRatingzScraper()
    returned = scraper.scrape_page(
        'https://www.drugratingz.com/reviews/18088/Drug-Affinitor.html')
    assert not returned
Exemplo n.º 9
0
def test_incorrect_url():
    """
    Tests that the scrape_page function raises and assertion error when a url is
    entered with an incorrect domain name (not 'https://drugratingz.com...')
    """
    scraper = DrugRatingzScraper()
    with pytest.raises(AssertionError):
        scraper.scrape_page(
            'https://www.drugs.com/comments/aripiprazole/abilify.html?page=1')
Exemplo n.º 10
0
def test_scrape_with_parameters():
    """
    Tests that, when calling the scrape function with a scraper of non-default parameters, the
    correct types of data are stored in the 'reviews' attribute
    """
    scraper = DrugRatingzScraper(collect_urls=True)
    scraper.scrape('https://www.drugratingz.com/reviews/472/Drug-Lutera.html')
    assert len(scraper.reviews) > 10
    data_collected = list(scraper.reviews[0].keys())
    assert len(data_collected) == 5
    assert 'url' in data_collected
Exemplo n.º 11
0
def test_scrape_page_with_parameters():
    """
    Tests to make sure that calling the scrape_page function on a scraper object with a
    non-default parameter (collect_url true) collects the correct types of data
    ('comment', 'rating, 'date', 'drug', and 'url')
    """
    scraper = DrugRatingzScraper(collect_urls=True)
    scraper.scrape_page(
        'https://www.drugratingz.com/reviews/141/Drug-Prednisone.html')
    data_collected = list(scraper.reviews[0].keys())
    assert len(data_collected) == 5
    assert 'url' in data_collected
Exemplo n.º 12
0
def test_drugratingz_scrape():
    """Test drug ratingz scrape"""
    input_url = 'https://www.drugratingz.com/reviews/75/Drug-Adderall-XR.html'
    drug_scraper = DrugRatingzScraper()
    review_list = drug_scraper.scrape(input_url)
    assert len(review_list) > 5

    keys = list(review_list[-1].keys())
    assert 'comment' in keys
    assert 'effectiveness' in keys
    assert 'no side effects' in keys
    assert 'convenience' in keys
    assert 'value' in keys
Exemplo n.º 13
0
    def collect(self, url, testing=False):
        """Scrapes drug reviews and saves them as dictionary property

        Args:
            url: WebMD URL where all the reviews are
        """
        if self.meta['locked']:
            print('Dataset locked. Please load a different dataset.')
            return

        self.meta['startTimestamp'] = time()
        self.meta['drugs'] = [self.drug_name]

        scraper = None
        if self.scraper == 'WebMD':
            scraper = WebMDScraper()
        elif self.scraper == 'EverydayHealth':
            scraper = EverydayHealthScraper()
        elif self.scraper == 'Drugs':
            scraper = DrugsScraper()
        elif self.scraper == 'DrugRatingz':
            scraper = DrugRatingzScraper()

        if testing:
            scraper = WebMDScraper(False, 1)

        self.reviews = scraper.scrape(url)
        self.meta['endTimestamp'] = time()
Exemplo n.º 14
0
def test_scrape_page_default_parameters():
    """
    Tests to make sure that calling the scrape_page function on a scraper object
    with a default parameter collects the correct types of data ('comment', 'rating',
    'date', and 'drug') and that the correct number of reviews were collected (13)
    """
    scraper = DrugRatingzScraper()
    scraper.scrape_page(
        'https://www.drugratingz.com/reviews/141/Drug-Prednisone.html')
    data_collected = list(scraper.reviews[0].keys())
    assert len(data_collected) == 4
    assert 'comment' in data_collected
    assert 'rating' in data_collected
    assert 'date' in data_collected
    assert 'drug' in data_collected
    assert len(scraper.reviews) == 13
Exemplo n.º 15
0
def test_scrape_correct_review_data():
    """
    Tests to make sure that the last review in the scraped reviews list has
    the correct data when the scrape function is called
    (this data is from the oldest review of the drug)
    """
    scraper = DrugRatingzScraper(collect_urls=True)
    scraper.scrape(
        'https://www.drugratingz.com/reviews/141/Drug-Prednisone.html')
    assert scraper.reviews[-1]['comment'][:10] == 'This is a '
    assert scraper.reviews[-1]['comment'][-10:] == 'ing on it.'
    assert scraper.reviews[-1]['rating']['effectiveness'] == 5
    assert scraper.reviews[-1]['rating']['no side effects'] == 1
    assert scraper.reviews[-1]['rating']['convenience'] == 1
    assert scraper.reviews[-1]['rating']['value'] == 3
    assert scraper.reviews[-1]['date'] == '8/18/05'
Exemplo n.º 16
0
def test_initialization_parameters():
    """
    Tests that when a new DrugRatingz scraper object is initialized with
    non default arguments the correct attributes are set (empty list[dict] 'reviews')
    and that the collect_urls attribute (boolean) is set to true
    """
    scraper = DrugRatingzScraper(collect_urls=True)
    assert scraper.collect_urls
Exemplo n.º 17
0
def test_default_initialization():
    """
    Tests that when the new scraper object is initialized, the correct
    attributes are set (empty list[dict] 'reviews') and the attribute
    collect_urls (boolean) is false by default
    """
    scraper = DrugRatingzScraper()
    assert len(scraper.reviews) == 0
    assert not scraper.collect_urls
Exemplo n.º 18
0
def test_scrape_empty_reviews():
    """
    Tests to make sure that the scrape function would discard the reviews
    of a scraper object that already has data collected in 'reviews'
    """
    scraper = DrugRatingzScraper()
    scraper.scrape(
        'https://www.drugratingz.com/reviews/258/Drug-Glucophage.html')
    num_reviews = len(scraper.reviews)
    scraper.scrape(
        'https://www.drugratingz.com/reviews/258/Drug-Glucophage.html')
    assert num_reviews == len(scraper.reviews)
Exemplo n.º 19
0
    def collect_drug_names(self, file_path, output_path):
        """Given list of drug names, collect urls for those review page on
            the scraper's website

        Args:
            file_path: input csv with list of drug names
            output_path: output csv with urls
        """
        if self.scraper == 'WebMD':
            scraper = WebMDScraper()
            scraper.get_drug_urls(file_path, output_path)
        elif self.scraper == 'EverydayHealth':
            scraper = EverydayHealthScraper()
            scraper.get_drug_urls(file_path, output_path)
        elif self.scraper == 'Drugs':
            scraper = DrugsScraper()
            scraper.get_drug_urls(file_path, output_path)
        elif self.scraper == 'DrugRatingz':
            scraper = DrugRatingzScraper()
            scraper.get_drug_urls(file_path, output_path)
Exemplo n.º 20
0
    def collect_urls(self, file_path, start=0):
        """Scrape all reviews for all drugs urls in file

        Args:
            start: index to start at if continuing from previous run
        """
        if self.meta['locked']:
            print('Dataset locked. Please load a different dataset.')
            return

        scraper = None
        if self.scraper == 'WebMD':
            scraper = WebMDScraper()
        elif self.scraper == 'EverydayHealth':
            scraper = EverydayHealthScraper()
        elif self.scraper == 'Drugs':
            scraper = DrugsScraper()
        elif self.scraper == 'DrugRatingz':
            scraper = DrugRatingzScraper()

        urls = []

        with open(file_path) as csv_file:
            reader = csv.DictReader(csv_file)
            for row in reader:
                if row['URL'] != 'Not found':
                    urls.append({'name': row['Drug'], 'url': row['URL']})
        print('Found {} urls.'.format(len(urls)))

        if os.path.isfile(self.drug_name.lower() + '-dataset.pickle'):
            self.load()
        else:
            print('Saving meta...')
            drug_names = [x['name'] for x in urls]
            self.meta['drugs'] = drug_names
            self.meta['startTimestamp'] = time()
            self.save()

        # Loop through urls starting at start index
        for i in range(start, len(urls)):
            drug = urls[i]
            print('\n{} drugs left to scrape.'.format(len(urls) - i))
            print('Scraping {}...'.format(drug['name']))
            reviews = scraper.scrape(drug['url'])

            # If it's the first drug then replace self.reviews instead of appending
            if drug['name'] == urls[0]['name']:
                self.reviews = reviews
            else:
                self.reviews += reviews

            # Save our progress and let the user know the data is safe
            self.meta['endTimestamp'] = time()
            self.save()
            print('{} reviews saved. Safe to quit.'.format(drug['name']))

            # Let the user know what start index to use to continue later
            if i < len(urls) - 1:
                print('To continue run with parameter start={}'.format(i + 1))

        print('\nAll urls scraped!')