-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlahmanlib.py
82 lines (65 loc) · 3.24 KB
/
lahmanlib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#lahmanlib
# Collection of modules written by Sean Lahman - seanlahman@gmail.com
# Created 2013-10-27
def HelloWorld():
print 'hello world'
def PatentScrape(p_id):
# ----------------------------------------------------------------------------
# Given the number of a US patent, scrape information about it from
# the USPTO website and return it as a data dictionary
# ----------------------------------------------------------------------------
#import libraries
from bs4 import BeautifulSoup # For processing HTML
import urllib2
#import sys
#set the URL based on the patent ID passed
url="http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO2&Sect2=HITOFF&u=%2Fnetahtml%2FPTO%2Fsearch-adv.htm&r=1&p=1&f=G&l=50&d=PTXT&S1="+p_id+".PN.&OS=pn/"+p_id+"&RS=PN/"+p_id
try:
#get webpage
downloaded_page = urllib2.urlopen(url)
soup = BeautifulSoup(downloaded_page)
#initialize variables
outdate=''
outinv=''
outass='-' #note, some patents have no assignee, so this state is likely not an error
#page through TD tags to find targeted text
mylist=soup.find_all('td')
count=1
#note: Loop uses length-3 since we are looking for markers to identify subsequent records, we don't want to iterate too far.
# in practice, we could stop after 25-30 or lines, but hardcoding that way could cause problems
# if USPTO changes their format in the future
while (count < (len(mylist)-3)):
count = count + 1
#look for markers
if 'United States Patent' in str(mylist[count]):
#patdate
patdate=(mylist[count+3])
outdate=patdate.find('b').string
outdate=str(outdate.strip())
if 'Inventors' in str(mylist[count]):
patinv=str(mylist[count+1])
outinv=patinv[patinv.index('90%')+5:-5]
outinv=outinv.strip()
outinv=outinv.replace('<b>','')
outinv=outinv.replace('</b>',' ')
outinv=outinv.replace(';',', ')
if 'Assignee' in str(mylist[count]):
patass=str(mylist[count+1])
outass=patass[patass.index('90%')+5:-15]
outass=outass.strip()
outass = "".join(outass.split('\n')) #strip newlines
outass=outass.replace('<b>','')
outass=outass.replace('</b>',' ')
outass=outass.replace('<br>','')
outass=outass.replace('<br>','')
#individual inventors have no assignee, so set value to "-"
if outass=='':
outass='-'
#all done, build dictionary and return what we found
patent_info = {'date' : outdate, 'inventor': outinv, 'assignee': outass}
return patent_info
except:
#The most rudimentary error handling possible
print "SL-Unexpected error:", sys.exc_info()[0]
patent_info = {'date' : "ERR", 'inventor': "ERR", 'assignee': "ERR"}
return patent_info