Skip to content

Instantly share code, notes, and snippets.

@mamacneil
Last active September 4, 2020 09:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamacneil/89ce74437d6392b026b39f190f335007 to your computer and use it in GitHub Desktop.
Save mamacneil/89ce74437d6392b026b39f190f335007 to your computer and use it in GitHub Desktop.
Grab K and age at maturity (tm) values from Fishbase Life History Page
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Grab k and tm life-history parameters from Fishbase Life History Data Page"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import bs4 \n",
"import urllib\n",
"import pandas as pd\n",
"import webbrowser\n",
"from selenium import webdriver\n",
"from selenium.webdriver.chrome.options import Options\n",
"import pdb\n",
"import time\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"def get_LH(spc):\n",
" chrome_options = Options() \n",
" chrome_options.add_argument(\"--headless\")\n",
" chrome_options.add_argument('--ignore-certificate-errors')\n",
" chrome_options.add_argument(\"--test-type\")\n",
" # Grab species page\n",
" urly = 'https://www.fishbase.se/summary/'+spc\n",
" driver1 = webdriver.Chrome(chrome_options=chrome_options, executable_path=r\"/Users/aaronmacneil/Dropbox/python_scripts/chromedriver\") #need to download ChromeDriver, http://chromedriver.chromium.org/downloads\n",
" driver1.implicitly_wait(30)\n",
" driver1.get(urly)\n",
" \n",
" # Grab Fishbase LifeHistory page\n",
" try:\n",
" urlx = driver1.find_element_by_partial_link_text('Life-history tool').get_attribute('href')\n",
" #print(urlx)\n",
"\n",
" driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=r\"/Users/aaronmacneil/Dropbox/python_scripts/chromedriver\") #need to download ChromeDriver, http://chromedriver.chromium.org/downloads\n",
" driver.implicitly_wait(30)\n",
" driver.get(urlx)\n",
" \n",
" \n",
" # Run Java to get K\n",
" k = driver.execute_script('return document.fn.finalk.value;')\n",
" tm = driver.execute_script('return document.fn.gtime.value;')\n",
" return float(k),float(tm)\n",
" \n",
" except UnexpectedAlertPresentException:\n",
" #pdb.set_trace()\n",
" driver.switch_to.active_element\n",
" k = driver.execute_script('return document.fn.finalk.value;')\n",
" tm = driver.execute_script('return document.fn.gtime.value;')\n",
" return k,tm"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/aaronmacneil/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:8: DeprecationWarning: use options instead of chrome_options\n",
" \n"
]
}
],
"source": [
"get_LH('Abalistes stellaris')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment