Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Python library for scraping using Selenium
If you are looking for the library implemented inside airflow, go to https://github.com/Avila-Systems/as-scraper-airflow.
The as-scraper library uses Geckodriver (Firefox) for scraping with the Selenium library. In order to use it, you need to have an Geckodriver dependency. Check the selenium documentation for details about how to install the Firefox browser driver.
Lets say that we want to scrap yellowpages.com. Our target data would be the popular cities that we can find in the sitemap url.
Our output data will have two columns: name
of the city and url
which is linked to the city. For example, for Houston, we would want the following output:
name | url |
---|---|
Houston | https://www.yellowpages.com/houston-tx |
So first we create a scraper that extends from the Scraper class, and define the COLUMNS
variable to ['name', 'url']
.
Create the scrapers/yellowpages.py file and type the following code into it:
from as_scraper.scraper import Scraper
class YellowPagesScraper(Scraper):
COLUMNS = ['name', 'url']
Now, there are two execution options when running scrapers. We can either load javascript which uses the Selenium library, or not load javascript and use the requests library for http requests.
For this example, let's go ahead and use the Selenium library. To configure this, simply add the following variable to your scraper:
from as_scraper.scraper import Scraper
class YellowPagesScraper(Scraper):
COLUMNS = ['name', 'url']
LOAD_JAVASCRIPT = True
scrape_handler
And the magic comes in the next step. We will define the scrape_handler
method in our class, which will have the responsibility to scrape a given url and extract the data from it.
All scrapers must define the
scrape_handler
method.
from typing import Optional
from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
import pandas as pd
from as_scraper.scraper import Scraper
class YellowPagesScraper(Scraper):
COLUMNS = ['name', 'url']
LOAD_JAVASCRIPT = True
def scrape_handler(self, url: str, html: Optional[str] = None, driver: Optional[Firefox] = None, **kwargs) -> pd.DataFrame:
rows = []
div_tag = driver.find_element(By.CLASS_NAME, "row-content")
div_tag = div_tag.find_element(By.CLASS_NAME, "row")
section_tags = div_tag.find_elements(By.TAG_NAME, "section")
for section_tag in section_tags:
a_tags = section_tag.find_elements(By.TAG_NAME, "a")
for a_tag in a_tags:
city_name = a_tag.text
city_url = a_tag.get_attribute("href")
rows.append({"name": city_name, "url": city_url})
df = pd.DataFrame(rows, columns=self.COLUMNS)
return df
Finally, to execute the scraper you must call the *execute method.
from typing import Optional
from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
import pandas as pd
from as_scraper.scraper import Scraper
class YellowPagesScraper(Scraper):
COLUMNS = ['name', 'url']
LOAD_JAVASCRIPT = True
def scrape_handler(self, url: str, html: Optional[str] = None, driver: Optional[Firefox] = None, **kwargs) -> pd.DataFrame:
rows = []
div_tag = driver.find_element(By.CLASS_NAME, "row-content")
div_tag = div_tag.find_element(By.CLASS_NAME, "row")
section_tags = div_tag.find_elements(By.TAG_NAME, "section")
for section_tag in section_tags:
a_tags = section_tag.find_elements(By.TAG_NAME, "a")
for a_tag in a_tags:
city_name = a_tag.text
city_url = a_tag.get_attribute("href")
rows.append({"name": city_name, "url": city_url})
df = pd.DataFrame(rows, columns=self.COLUMNS)
return df
if __name__ == '__main__':
urls = ['https://www.yellowpages.com/sitemap']
scraper = YellowPagesScraper(urls)
results, errors = scraper.execute()
print(results)
print(errors)
Now go ahead and run python scrapers/yellowpages.py
. Have fun!
FAQs
Python library for scraping with Selenium.
We found that as-scraper demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.