Webscraping the Indian Patent Website for patent data
<p>I am trying to write a webscraper for the <a href="http://ipindiaservices.gov.in/publicsearch/" rel="nofollow">Indian patent search website</a> to get data about patents. Here is the code that I have so far.</p>

<pre><code>#import the necessary modules
import urllib2
#import the beautifulsoup functions to parse the data
from bs4 import BeautifulSoup

#mention the website that you are trying to scrape
patentsite="http://ipindiaservices.gov.in/publicsearch/"

#Query the website and return the html to the variable 'page'
page = urllib2.urlopen(patentsite)

#Parse the html in the 'page' variable, and store it in Beautiful Soup format
soup = BeautifulSoup(page)

print soup
</code></pre>

<p>Unfortunately, the Indian patent website is not robust or I am not sure how to proceed further in this regard. </p>

<p>This is the output for the above code.</p>

<pre><code>&lt;!-- 
################################################################### 
##                                                               ##
##                                                               ##
##           SIDDHAST.COM                                        ##            
##                                                               ##
##                                                               ##
################################################################### 
--&gt;&lt;!DOCTYPE HTML&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta content="IE=edge" http-equiv="X-UA-Compatible"/&gt;
&lt;meta charset="utf-8"/&gt;
&lt;title&gt;:: InPASS - Indian Patent Advanced Search System ::&lt;/title&gt;
&lt;link href="resources/ipats-all.css" rel="stylesheet"/&gt;
&lt;script src="app.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;link href="resources/app.css" rel="stylesheet"/&gt;
&lt;/head&gt;
&lt;body&gt;&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>The thing that I want to give is, suppose I provide a company name, the scraper should get all the patents for that particular company. I want to do other things if I can get this part right, like providing a set of inputs that the scraper will use to find patents. But I am stuck at the part where I am unable to proceed further.</p>

<p>Any pointers on how to get this data will be greatly appreciated.</p>

<p>You can do this with just <em>requests</em>. The post is to <em><a href="http://ipindiaservices.gov.in/publicsearch/resources/webservices/search.php" rel="nofollow">http://ipindiaservices.gov.in/publicsearch/resources/webservices/search.php</a></em> with one <em>param</em> <em>rc_</em> which is a timestamp that we create with <em>time.time</em>.</p>

<p>Each value in <code>"field[]"</code> should match up to each in <code>"fieldvalue[]"</code> and in turn match to <code>"operator[]"</code> whether you chose to <code>*AND*</code> <code>*OR*</code> or <code>*NOT*</code>, the <code>[]</code> after each key specifies that we are passing an array of <em>value(s)</em>, without that nothing would work.:</p>

<pre><code>data = {
    "publication_type_published": "on",
    "publication_type_granted": "on",
    "fieldDate": "APD",
    "datefieldfrom": "19120101",
    "datefieldto": "20160906",
    "operatordate": " AND ",
    "field[]": ["PA"], # claims,.description, patent-number codes go here
    "fieldvalue[]": ["chris*"], # matching values for ^^ go here
    "operator[]": [" AND "], # matching sql logic for ^^ goes here
    "page": "1", #  gives you next page results
    "start": "0", # not sure what effect this actually has.
    "limit": "25"} # not sure how this relates as  len(r.json()[u'record']) stays 25 regardless

import requests
from time import time

post = "http://ipindiaservices.gov.in/publicsearch/resources/webservices/search.php?_dc={}".format(
    str(time()).replace(".", ""))

with requests.Session() as s:
    s.get("http://ipindiaservices.gov.in/publicsearch/")
    s.headers.update({"X-Requested-With": "XMLHttpRequest"})
    r = s.post(post, data=data)
    print(r.json())
</code></pre>

<p>Output will look like the following, I cannot add it all as there is too much data to post:</p>

<pre><code>{u'success': True, u'record': [{u'Publication_Status': u'Published', u'appDate': u'2016/06/16', u'pubDate': u'2016/08/31', u'title': u'ACTUATOR FOR DEPLOYABLE IMPLANT', u'sourceID': u'inpat', u'abstract': u'\n    Systems and methods are provided for usin.............
</code></pre>

<p>If you use the record key you get a list of dicts like:</p>

<pre><code>{u'Publication_Status': u'Published', u'appDate': u'2015/01/27', u'pubDate': u'2015/06/26', u'title': u'CORRUGATED PALLET', u'sourceID': u'inpat', u'abstract': u'\n    A corrugated paperboard pallet is produced from two flat blanks which comprise a pallet top and a pallet bottom. The two blanks are each folded to produce only two parallel vertically extending double thickness ribs&amp;nbsp;three horizontal panels&amp;nbsp;two vertical side walls and two horizontal flaps. The ribs of the pallet top and pallet bottom lock each other from opening in the center of the pallet by intersecting perpendicularly with notches in the ribs. The horizontal flaps lock the ribs from opening at the edges of the pallet by intersecting perpendicularly with notches&amp;nbsp;and the vertical sidewalls include vertical flaps that open inward defining fork passages whereby the vertical flaps lock said horizontal flaps from opening.\n  ', u'Assignee': u'OLVEY Douglas A., SKETO James L., GUMBERT Sean G., DANKO Joseph J., GABRYS Christopher W., ', u'field_of_invention': u'FI10', u'publication_no': u'26/2015', u'patent_no': u'', u'application_no': u'642/DELNP/2015', u'UCID': u'WVJ4NVVIYzFLcUQvVnJsZGczcVRmSS96Vkh3NWsrS1h3Qk43S2xHczJ2WT0%3D', u'Publication_Type': u'A'}
</code></pre>

<p>which is your patent info.</p>

<p>You can see if we choose a few values in our browser, the values in all <em>fieldvalue</em>, <em>field</em> and <em>operator</em>  line up, <code>AND</code>is the default so you see that for every option:</p>

<p><a href="http://i.stack.imgur.com/V6ni6.png" rel="nofollow"><img src="http://i.stack.imgur.com/V6ni6.png" alt="enter image description here"></a></p>

<p><a href="http://i.stack.imgur.com/UwHnE.png" rel="nofollow"><img src="http://i.stack.imgur.com/UwHnE.png" alt="enter image description here"></a></p>

<p>So figure out the code, pick what you want and post.</p>

python python-2.7 web-scraping beautifulsoup