forked from ryanlstevens/google_patent_scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape2.py
More file actions
309 lines (265 loc) · 13.7 KB
/
Copy pathscrape2.py
File metadata and controls
309 lines (265 loc) · 13.7 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# main.py
import asyncio
import json
import requests
from bs4 import BeautifulSoup
from playwright.async_api import async_playwright, TimeoutError as PlaywrightTimeoutError
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# Custom Errors
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
class PatentClassError(Exception):
"""Custom exception for errors in patent class usage."""
pass
class NoPatentsError(Exception):
"""Custom exception for when no patents are provided to scrape."""
pass
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# Create scraper class
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
class scraper_class:
"""
Asynchronous Google scraper class using Playwright to scrape data from 'https://patents.google.com/'.
This version uses the async API of Playwright to fetch patents concurrently,
which is significantly faster for multiple patents.
Usage:
async def main():
# Use 'async with' to manage the browser lifecycle
async with scraper_class(return_abstract=True, return_claims=True, return_description=True) as scraper:
scraper.add_patents('US2668287A')
scraper.add_patents('US8834455B2')
# This will now run all scraping tasks concurrently
await scraper.scrape_all_patents()
print(json.dumps(scraper.parsed_patents, indent=2))
if __name__ == "__main__":
asyncio.run(main())
"""
def __init__(self, return_abstract=False, return_description=False, return_claims=False, headless=True):
"""Initializes the scraper's configuration."""
self.list_of_patents = []
self.scrape_status = {}
self.parsed_patents = {}
self.return_abstract = return_abstract
self.return_description = return_description
self.return_claims = return_claims
self.headless = headless
self.playwright = None
self.browser = None
async def __aenter__(self):
"""
Asynchronous context manager entry.
Initializes Playwright and the browser.
"""
print("Starting Playwright and launching browser...")
self.playwright = await async_playwright().start()
self.browser = await self.playwright.chromium.launch(headless=self.headless)
print("Browser launched.")
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
"""
Asynchronous context manager exit.
Ensures resources are closed properly.
"""
print("Closing browser and Playwright resources...")
if self.browser:
await self.browser.close()
if self.playwright:
await self.playwright.stop()
print("Resources closed.")
def add_patents(self, patent):
"""Appends a patent to the list to be scraped."""
if not isinstance(patent, str):
raise PatentClassError("'patent' variable must be a string")
self.list_of_patents.append(patent)
def delete_patents(self, patent):
"""Removes a patent from the list."""
if patent in self.list_of_patents:
self.list_of_patents.remove(patent)
else:
print(f'Patent {patent} not in patent list')
def add_scrape_status(self, patent, success_value):
"""Adds the status of a scrape to the dictionary."""
self.scrape_status[patent] = success_value
async def request_single_patent(self, patent):
"""
Fetches a single patent page asynchronously.
Args:
patent (str): The patent number.
Returns:
tuple: A tuple containing the patent number and the result dictionary.
The result will contain either the parsed data or an error message.
"""
initial_url = f"https://patents.google.com/?oq={patent}"
print(f"🚀 Starting scrape for: {patent} at {initial_url}")
page = None
try:
page = await self.browser.new_page()
await page.goto(initial_url, wait_until='load', timeout=60000)
final_url = page.url
print(f"➡️ Redirected to: {final_url} for patent {patent}")
# *** FIX: Explicitly wait for a key element to be visible. ***
# This is the crucial step. We wait for the element containing the publication
# date to appear. This ensures the page's JavaScript has finished rendering
# the dynamic content before we try to parse the HTML.
# print(f"⏳ Waiting for content to load for patent {patent}...")
# await page.wait_for_selector('dd[itemprop="assigneeCurrent"]', timeout=10000)
# print(f"✅ Content loaded for patent {patent}.")
# html_content = await page.content()
# soup = BeautifulSoup(html_content, "lxml")
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.get(final_url, headers=headers, timeout=20)
response.raise_for_status() # Raises HTTPError for bad responses (4xx or 5xx)
soup = BeautifulSoup(response.content, features="lxml")
# Process the data and return it
parsed_data = self.get_scraped_data(soup, patent, final_url)
self.add_scrape_status(patent, 'Success')
await page.close()
return patent, parsed_data
except PlaywrightTimeoutError:
error_msg = f'Timeout Error: Could not find key content on page {final_url}. The page might not be a valid patent page or took too long to load.'
print(f'❌ Patent: {patent}, {error_msg}')
if page: await page.close()
self.add_scrape_status(patent, error_msg)
return patent, {'error': error_msg}
except Exception as e:
error_msg = f'An unexpected error occurred: {e}'
print(f'❌ Patent: {patent}, {error_msg}')
if page: await page.close()
self.add_scrape_status(patent, error_msg)
return patent, {'error': error_msg}
def parse_citation(self, single_citation):
"""Parses a single patent citation from a table row element."""
try:
patent_number = single_citation.find('span', itemprop='publicationNumber').get_text(strip=True)
except AttributeError:
patent_number = ''
try:
priority_date = single_citation.find('td', itemprop='priorityDate').get_text(strip=True)
except AttributeError:
priority_date = ''
try:
publication_date = single_citation.find('td', itemprop='publicationDate').get_text(strip=True)
except AttributeError:
publication_date = ''
return {'patent_number': patent_number, 'priority_date': priority_date, 'publication_date': publication_date}
def process_patent_html(self, soup):
"""Parses the full HTML of a patent page to extract key information."""
try:
# Using a more specific selector for the title
# title_element = soup.find('h1', id='title')
# title_text = title_element.get_text(strip=True) if title_element else ''
title = soup.find('meta',attrs={'name':'DC.title'})
title_text=title['content'].rstrip()
except (TypeError, KeyError, AttributeError):
title_text = ''
inventor_name = [x.get_text(strip=True) for x in soup.find_all('dd', itemprop='inventor')]
assignee_name_orig = [x.get_text(strip=True) for x in soup.find_all('dd', itemprop='assigneeOriginal')]
assignee_name_current = [x.get_text(strip=True) for x in soup.find_all('dd', itemprop='assigneeCurrent')]
try:
publication_date = soup.find('dd', itemprop='publicationDate').get_text(strip=True)
except AttributeError:
publication_date = ''
try:
publication_number = soup.find('dd',itemprop="publicationNumber").get_text()
except:
publication_number = ''
try:
application_number = soup.find('dd', itemprop="applicationNumber").get_text(strip=True)
except AttributeError:
application_number = ''
try:
filing_date_element = soup.find('dd', itemprop='filingDate')
filing_date = filing_date_element.find('time').get_text(strip=True) if filing_date_element else ''
except AttributeError:
filing_date = ''
try:
legal_status_ifi = soup.find('dd', itemprop='legalStatusIfi').get_text(strip=True)
except AttributeError:
legal_status_ifi = ''
priority_date, granted_date, expiration_date = '', '', ''
for event in soup.find_all('dd', itemprop='events'):
try:
event_type = event.find('span', itemprop='type').get_text(strip=True)
event_date = event.find('time', itemprop='date').get_text(strip=True)
if event_type == 'priority': priority_date = event_date
elif event_type == 'granted': granted_date = event_date
elif event_type == 'publication' and not publication_date: publication_date = event_date
event_title_span = event.find('span', itemprop='title')
if event_title_span and 'expiration' in event_title_span.get_text(strip=True).lower():
expiration_date = event_date
except AttributeError:
continue
forward_cites_no_family = [self.parse_citation(c) for c in soup.find_all('tr', itemprop="forwardReferencesOrig")]
forward_cites_yes_family = [self.parse_citation(c) for c in soup.find_all('tr', itemprop="forwardReferencesFamily")]
backward_cites_no_family = [self.parse_citation(c) for c in soup.find_all('tr', itemprop='backwardReferences')]
backward_cites_yes_family = [self.parse_citation(c) for c in soup.find_all('tr', itemprop='backwardReferencesFamily')]
classifications = []
for item in soup.find_all('li', itemprop='classifications'):
if item.find('meta', itemprop='Leaf', content='true'):
try:
code = item.find('span', itemprop='Code').get_text(strip=True)
description = item.find('span', itemprop='Description').get_text(strip=True)
classifications.append({'code': code, 'description': description})
except AttributeError:
continue
abstract_text, description_text, claims_text = '', '', ''
if self.return_abstract:
abstract_element = soup.select_one("section.abstract > div.abstract")
abstract_text = abstract_element.text.strip() if abstract_element else "Abstract not found"
if self.return_description:
description_html = soup.find('section', itemprop='description')
description_text = description_html.get_text(separator='\n', strip=True) if description_html else ""
if self.return_claims:
claims_html = soup.find('section', itemprop='claims')
claims_text = claims_html.get_text(separator='\n', strip=True) if claims_html else ""
return {
'title': title_text,
'inventor_name': json.dumps(inventor_name, ensure_ascii=False),
'assignee_name_orig': json.dumps(assignee_name_orig, ensure_ascii=False),
'assignee_name_current': json.dumps(assignee_name_current, ensure_ascii=False),
'publication_date': publication_date,
'priority_date': priority_date,
'granted_date': granted_date,
'filing_date': filing_date,
'expiration_date': expiration_date,
'application_number': application_number,
'publication_number': publication_number,
'legal_status': legal_status_ifi,
'forward_cite_no_family': json.dumps(forward_cites_no_family, ensure_ascii=False),
'forward_cite_yes_family': json.dumps(forward_cites_yes_family, ensure_ascii=False),
'backward_cite_no_family': json.dumps(backward_cites_no_family, ensure_ascii=False),
'backward_cite_yes_family': json.dumps(backward_cites_yes_family, ensure_ascii=False),
'classifications': json.dumps(classifications, ensure_ascii=False),
'abstract_text': abstract_text,
'description_text': description_text,
'claims_text': claims_text,
}
def get_scraped_data(self, soup, patent, url):
"""Processes the soup and adds metadata."""
parsed_data = self.process_patent_html(soup)
parsed_data['url'] = url
parsed_data['patent'] = patent
return parsed_data
async def scrape_all_patents(self):
"""
Scrapes all patents in the list concurrently.
"""
if not self.list_of_patents:
raise NoPatentsError("No patents to scrape. Add patents using scraper.add_patents()")
tasks = [self.request_single_patent(patent) for patent in self.list_of_patents]
results = await asyncio.gather(*tasks)
for patent_id, data in results:
self.parsed_patents[patent_id] = data
async def main():
"""Main function to run the scraper."""
async with scraper_class(return_abstract=True, return_claims=0, return_description=0, headless=False) as scraper:
scraper.add_patents('US2014262394')
# scraper.add_patents('US8834455B2')
# scraper.add_patents('US-11000000-B2')
# scraper.add_patents('WO2022060606A1')
await scraper.scrape_all_patents()
print("\n--- ✅ Scraping Results ---")
print(json.dumps(scraper.parsed_patents, indent=4))
print("\n--- 📊 Scrape Status ---")
print(json.dumps(scraper.scrape_status, indent=4))
if __name__ == "__main__":
asyncio.run(main())