Skip to content

Instantly share code, notes, and snippets.

@kozo2
Last active January 13, 2016 21:12
Show Gist options
  • Select an option

  • Save kozo2/25f8accadd9b6d3965b4 to your computer and use it in GitHub Desktop.

Select an option

Save kozo2/25f8accadd9b6d3965b4 to your computer and use it in GitHub Desktop.
import glob
from bs4 import BeautifulSoup
import bs4
htmls = glob.glob('./knapsack/C*')
output = open("knapsack.tsv", "w")
output.write("\t".join(["knapsackid", "name", "formula", "mw", "cas", "inchikey"]))
output.write("\n")
for i in htmls:
knapsackid = i.split("\\")[-1]
f = open(i)
tags = f.read()
soup = BeautifulSoup(tags, 'html.parser')
title = soup.title.string.split(" - ")[-1]
for j in soup.td.find_all('tr'):
if type(j.th) == bs4.element.Tag:
if j.th.string == "Formula":
formula = j.td.string
elif j.th.string == "Mw":
mw = j.td.string
elif j.th.string == "CAS RN":
if j.td.string:
cas = j.td.string
elif j.th.string == "InChIKey":
if j.td.string:
inchikey = j.td.string
output.write("\t".join([knapsackid, title, formula, mw, cas, inchikey]))
output.write("\n")
output.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment