Last active
January 13, 2016 21:12
-
-
Save kozo2/25f8accadd9b6d3965b4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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