Created
November 29, 2012 21:32
-
-
Save kxtells/4172052 to your computer and use it in GitHub Desktop.
KEGG get target from drug
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
#!/usr/bin/python | |
import urllib2 | |
from HTMLParser import HTMLParser | |
def getPage(drugId): | |
url = "http://www.genome.jp/dbget-bin/get_linkdb?-t+8+dr:"+drugId | |
req = urllib2.Request(url) | |
response = urllib2.urlopen(req) | |
return response.read() | |
class genome_jp_HTMLParser(HTMLParser): | |
def __init__(self): | |
HTMLParser.__init__(self) | |
self.common_href = "/dbget-bin/www_bget" | |
self.hits = 0 | |
self.info = {} | |
def handle_starttag(self, tag, attrs): | |
if tag == "a": | |
attrs = dict(attrs) | |
href = attrs.get("href") | |
if href.find(self.common_href) != -1: | |
self.hits += 1 | |
#print "Encountered a start tag:", tag | |
def handle_endtag(self, tag): | |
pass | |
def handle_data(self, data): | |
if self.hits == 1: | |
self.key = data | |
self.hits += 1 | |
elif self.hits == 2: | |
self.info[self.key] = data.replace(" ","") | |
self.hits = 0 | |
def getResults(self): | |
return self.info | |
data = getPage("D00109") | |
parser = genome_jp_HTMLParser() | |
parser.feed(data) | |
print parser.getResults() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment