import React, { useState, useEffect } from 'react'; import { useNavigate } from…
- User
import React, { useState, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import './Home.css';
const ResultCard = ({ result, onAddToLibrary, cardNumber }) => { return ( {cardNumber}. <a href={
http://192.168.2.5:3006/files/${result.relativePath}} target="_blank" rel="noopener noreferrer" > {result.Filename.split('\').pop()} <button onClick={() => onAddToLibrary(result)} className="add-button"> Add {result.matches.length > 0 ? ( {result.matches.map((match, index) => ( Page {match.page} <div className="snippet" dangerouslySetInnerHTML={{ __html: match.snippet.replace( new RegExp((${match.keyword}), 'gi'), '$1' ), }} /> ))} ) : ( No matches found )} ); };const Home = () => { const [searchTerm, setSearchTerm] = useState(''); const [excludeWords, setExcludeWords] = useState(''); const [searchResults, setSearchResults] = useState([]); const [error, setError] = useState(null); const [isLoading, setIsLoading] = useState(false); const [filteredFilenames, setFilteredFilenames] = useState([]); const [fileInput, setFileInput] = useState(''); const navigate = useNavigate();
const filenames = [ { name: 'Compilation of Judgments - Claimant', path: '\\001. Miscellaneous\\51. Section 16 Arbitration Act\\Compilation of Judgments- Claimant.pdf', }, { name: 'Xerox Corporation and Ors vs PK Khansaheb and Ors', path: '\\001. Miscellaneous\\57.Additional documents\\Xerox_Corporation_and_Ors_vs_PK_Khansaheb_and_Ors_DE201819121816353171COM57952.pdf', }, { name: 'Compilation of Judgments - Claimant', path: '\\001. Miscellaneous\\51. Section 16 Arbitration Act\\Compilation of Judgments- Claimant.pdf', }, { name: 'Xerox Corporation and Ors vs PK Khansaheb and Ors', path: '\\001. Miscellaneous\\57.Additional documents\\Xerox_Corporation_and_Ors_vs_PK_Khansaheb_and_Ors_DE201819121816353171COM57952.pdf', }, { name: 'Compilation of Judgments - Claimant', path: '\\001. Miscellaneous\\51. Section 16 Arbitration Act\\Compilation of Judgments- Claimant.pdf', }, { name: 'Xerox Corporation and Ors vs PK Khansaheb and Ors', path: '\\001. Miscellaneous\\57.Additional documents\\Xerox_Corporation_and_Ors_vs_PK_Khansaheb_and_Ors_DE201819121816353171COM57952.pdf', }, // Add other filenames here in the same format... ]; useEffect(() => { setFilteredFilenames(filenames); }, []); const handleFileInputChange = (event) => { const value = event.target.value; setFileInput(value); if (value) { setFilteredFilenames( filenames.filter((file) => file.name.toLowerCase().includes(value.toLowerCase()) ) ); } else { setFilteredFilenames(filenames); } }; const handleSubmit = async (event) => { event.preventDefault(); if (!searchTerm.trim()) { setError('Please enter a search term.'); return; } setIsLoading(true); setError(null); try { const response = await fetch( `http://192.168.2.5:3006/search?keyword=${encodeURIComponent( searchTerm )}&exclude=${encodeURIComponent(excludeWords || '')}` ); if (!response.ok) throw new Error('Failed to fetch search results'); const data = await response.json(); setSearchResults(data); } catch (error) { console.error(error); setError('Error fetching search results.'); } finally { setIsLoading(false); } }; const handleAddToLibrary = async (pdf) => { const token = localStorage.getItem('token'); if (!token) { navigate('/login', { state: { message: 'Please log in to add documents to your library', redirectAfterLogin: '/home', }, }); return; } try { const response = await fetch( 'http://192.168.2.5:3006/api/lib/library/add', { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}`, }, body: JSON.stringify({ filename: pdf.relativePath, originalFilePath: pdf.Filename, }), } ); if (response.ok) { alert('Document added to library'); } else { const errorData = await response.json(); alert(errorData.message || 'Failed to add document to library'); } } catch (error) { console.error('Error adding to library:', error); alert('Network error. Please try again.'); } }; return ( <div className="app-container"> {/* Sidebar */} <div className="sidebar"> <div className="sidebar-header"> <h2>S&A Law Offices</h2> </div> <div className="sidebar-content"> <div className="dropdown"> <input type="text" placeholder="Type a file name to search..." value={fileInput} onChange={handleFileInputChange} className="sidebar-input" /> {/* Dropdown for filtered filenames */} {fileInput && ( <div className="dropdown-content show"> {filteredFilenames.length > 0 ? ( filteredFilenames.map((file, index) => { // Process the path to extract relativePath const relativePath = file.path .replace(/\\\\/g, '/') // Convert backslashes to forward slashes .replace(/.*GGN Office Repository\//, ''); // Strip base path return ( <a key={index} href={`http://192.168.2.5:3006/files/${encodeURIComponent(relativePath)}`} target="_blank" rel="noopener noreferrer" className="suggestion-item" > {file.name} </a> ); }) ) : ( <div className="no-suggestions">No matches found</div> )} </div> )} </div> </div>{/* Main Content */} <div className="main-content"> <div className="search-container"> <form onSubmit={handleSubmit} className="search-form"> <input type="text" placeholder="Enter search term" value={searchTerm} onChange={(e) => setSearchTerm(e.target.value)} className="search-input" /> <input type="text" placeholder="Exclude words (optional)" value={excludeWords} onChange={(e) => setExcludeWords(e.target.value)} className="search-input" /> <button type="submit" disabled={isLoading} className="search-button" > {isLoading ? 'Searching...' : 'Search'} </button> </form> {error && <div className="error-message">{error}</div>} <div className="results-section"> {isLoading ? ( <div className="loading">Loading...</div> ) : searchResults.length > 0 ? ( <> <div className="results-count"> Found {searchResults.length} documents </div> <div className="results-list"> {searchResults.map((result, index) => ( <ResultCard key={index} result={result} onAddToLibrary={handleAddToLibrary} cardNumber={index + 1} /> ))} </div> </> ) : ( <div className="no-results">No results found</div> )} </div> </div> </div> </div> );};
export default Home; in tis code below the serach dropdown...i want two radio buttpns...one is ISSues and othet is Judgements
- Assistant
You can add two radio buttons labeled "Issues" and "Judgments" under the search input field in your React component. This will allow users to select a search category. Below is how you can implement this in your
Homecomponent:- Add a state to manage the selected search type.
- Add the radio buttons to the JSX of your
Homefunction.
Here's how you can modify your component accordingly:
import React, { useState, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import './Home.css'; const ResultCard = ({ result, onAddToLibrary, cardNumber }) => { return ( <div className="result-card"> <div className="result-header"> <h3> <span className="result-number">{cardNumber}.</span> <a href={`http://192.168.2.5:3006/files/${result.relativePath}`} target="_blank" rel="noopener noreferrer" > {result.Filename.split('\\').pop()} </a> </h3> <button onClick={() => onAddToLibrary(result)} className="add-button"> Add </button> </div> {result.matches.length > 0 ? ( <div className="matches"> {result.matches.map((match, index) => ( <div key={index} className="match-item"> <div>Page {match.page}</div> <div className="snippet" dangerouslySetInnerHTML={{ __html: match.snippet.replace( new RegExp(`(${match.keyword})`, 'gi'), '<mark>$1</mark>' ), }} /> </div> ))} </div> ) : ( <p className="no-matches">No matches found</p> )} </div> ); }; const Home = () => { const [searchTerm, setSearchTerm] = useState(''); const [excludeWords, setExcludeWords] = useState(''); const [searchResults, setSearchResults] = useState([]); const [error, setError] = useState(null); const [isLoading, setIsLoading] = useState(false); const [filteredFilenames, setFilteredFilenames] = useState([]); const [fileInput, setFileInput] = useState(''); const [searchType, setSearchType] = useState('issues'); // State for search type const navigate = useNavigate(); // ... (existing useEffect and filenames) const handleSearchTypeChange = (event) => { setSearchType(event.target.value); }; const handleSubmit = async (event) => { event.preventDefault(); if (!searchTerm.trim()) { setError('Please enter a search term.'); return; } setIsLoading(true); setError(null); // Including searchType in the query parameters try { const response = await fetch( `http://192.168.2.5:3006/search?keyword=${encodeURIComponent( searchTerm )}&exclude=${encodeURIComponent(excludeWords || '')}&type=${encodeURIComponent(searchType)}` ); if (!response.ok) throw new Error('Failed to fetch search results'); const data = await response.json(); setSearchResults(data); } catch (error) { console.error(error); setError('Error fetching search results.'); } finally { setIsLoading(false); } }; return ( <div className="app-container"> {/* Sidebar */} <div className="sidebar"> <div className="sidebar-header"> <h2>S&A Law Offices</h2> </div> <div className="sidebar-content"> <div className="dropdown"> <input type="text" placeholder="Type a file name to search..." value={fileInput} onChange={handleFileInputChange} className="sidebar-input" /> {/* Dropdown for filtered filenames */} {fileInput && ( <div className="dropdown-content show"> {filteredFilenames.length > 0 ? ( filteredFilenames.map((file, index) => { const relativePath = file.path .replace(/\\\\/g, '/') .replace(/.*GGN Office Repository\//, ''); return ( <a key={index} href={`http://192.168.2.5:3006/files/${encodeURIComponent(relativePath)}`} target="_blank" rel="noopener noreferrer" className="suggestion-item" > {file.name} </a> ); }) ) : ( <div className="no-suggestions">No matches found</div> )} </div> )} </div> </div> </div> {/* Main Content */} <div className="main-content"> <div className="search-container"> <form onSubmit={handleSubmit} className="search-form"> <input type="text" placeholder="Enter search term" value={searchTerm} onChange={(e) => setSearchTerm(e.target.value)} className="search-input" /> <input type="text" placeholder="Exclude words (optional)" value={excludeWords} onChange={(e) => setExcludeWords(e.target.value)} className="search-input" /> <div className="search-type"> <label> <input type="radio" value="issues" checked={searchType === 'issues'} onChange={handleSearchTypeChange} /> Issues </label> <label> <input type="radio" value="judgments" checked={searchType === 'judgments'} onChange={handleSearchTypeChange} /> Judgments </label> </div> <button type="submit" disabled={isLoading} className="search-button" > {isLoading ? 'Searching...' : 'Search'} </button> </form> {error && <div className="error-message">{error}</div>} <div className="results-section"> {isLoading ? ( <div className="loading">Loading...</div> ) : searchResults.length > 0 ? ( <> <div className="results-count"> Found {searchResults.length} documents </div> <div className="results-list"> {searchResults.map((result, index) => ( <ResultCard key={index} result={result} onAddToLibrary={handleAddToLibrary} cardNumber={index + 1} /> ))} </div> </> ) : ( <div className="no-results">No results found</div> )} </div> </div> </div> </div> ); }; export default Home;Key Modifications:
- State for Search Type: The
searchTypestate variable is added to manage which radio button is selected. - Radio Button Implementation: Two radio buttons are situated within a
divfor theIssuesandJudgmentsoptions. - Update Fetch Request: The
searchTypeis included in the fetch URL as a query parameter to determine the type of search.
This implementation will render two radio buttons under the search input field and functionality to handle search based on the selected type.
- User
prboide comlete upadetd code
- Assistant
Here's the complete updated code for your React component with the radio buttons for "Issues" and "Judgments" included in the search functionality:
import React, { useState, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import './Home.css'; const ResultCard = ({ result, onAddToLibrary, cardNumber }) => { return ( <div className="result-card"> <div className="result-header"> <h3> <span className="result-number">{cardNumber}.</span> <a href={`http://192.168.2.5:3006/files/${result.relativePath}`} target="_blank" rel="noopener noreferrer" > {result.Filename.split('\\').pop()} </a> </h3> <button onClick={() => onAddToLibrary(result)} className="add-button"> Add </button> </div> {result.matches.length > 0 ? ( <div className="matches"> {result.matches.map((match, index) => ( <div key={index} className="match-item"> <div>Page {match.page}</div> <div className="snippet" dangerouslySetInnerHTML={{ __html: match.snippet.replace( new RegExp(`(${match.keyword})`, 'gi'), '<mark>$1</mark>' ), }} /> </div> ))} </div> ) : ( <p className="no-matches">No matches found</p> )} </div> ); }; const Home = () => { const [searchTerm, setSearchTerm] = useState(''); const [excludeWords, setExcludeWords] = useState(''); const [searchResults, setSearchResults] = useState([]); const [error, setError] = useState(null); const [isLoading, setIsLoading] = useState(false); const [filteredFilenames, setFilteredFilenames] = useState([]); const [fileInput, setFileInput] = useState(''); const [searchType, setSearchType] = useState('issues'); // State for search type const navigate = useNavigate(); const filenames = [ { name: 'Compilation of Judgments - Claimant', path: '\\001. Miscellaneous\\51. Section 16 Arbitration Act\\Compilation of Judgments- Claimant.pdf', }, { name: 'Xerox Corporation and Ors vs PK Khansaheb and Ors', path: '\\001. Miscellaneous\\57.Additional documents\\Xerox_Corporation_and_Ors_vs_PK_Khansaheb_and_Ors_DE201819121816353171COM57952.pdf', }, // Add other filenames here in the same format... ]; useEffect(() => { setFilteredFilenames(filenames); }, []); const handleFileInputChange = (event) => { const value = event.target.value; setFileInput(value); if (value) { setFilteredFilenames( filenames.filter((file) => file.name.toLowerCase().includes(value.toLowerCase()) ) ); } else { setFilteredFilenames(filenames); } }; const handleSearchTypeChange = (event) => { setSearchType(event.target.value); }; const handleSubmit = async (event) => { event.preventDefault(); if (!searchTerm.trim()) { setError('Please enter a search term.'); return; } setIsLoading(true); setError(null); try { const response = await fetch( `http://192.168.2.5:3006/search?keyword=${encodeURIComponent( searchTerm )}&exclude=${encodeURIComponent(excludeWords || '')}&type=${encodeURIComponent(searchType)}` ); if (!response.ok) throw new Error('Failed to fetch search results'); const data = await response.json(); setSearchResults(data); } catch (error) { console.error(error); setError('Error fetching search results.'); } finally { setIsLoading(false); } }; const handleAddToLibrary = async (pdf) => { const token = localStorage.getItem('token'); if (!token) { navigate('/login', { state: { message: 'Please log in to add documents to your library', redirectAfterLogin: '/home', }, }); return; } try { const response = await fetch( 'http://192.168.2.5:3006/api/lib/library/add', { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}`, }, body: JSON.stringify({ filename: pdf.relativePath, originalFilePath: pdf.Filename, }), } ); if (response.ok) { alert('Document added to library'); } else { const errorData = await response.json(); alert(errorData.message || 'Failed to add document to library'); } } catch (error) { console.error('Error adding to library:', error); alert('Network error. Please try again.'); } }; return ( <div className="app-container"> {/* Sidebar */} <div className="sidebar"> <div className="sidebar-header"> <h2>S&A Law Offices</h2> </div> <div className="sidebar-content"> <div className="dropdown"> <input type="text" placeholder="Type a file name to search..." value={fileInput} onChange={handleFileInputChange} className="sidebar-input" /> {/* Dropdown for filtered filenames */} {fileInput && ( <div className="dropdown-content show"> {filteredFilenames.length > 0 ? ( filteredFilenames.map((file, index) => { const relativePath = file.path .replace(/\\\\/g, '/') .replace(/.*GGN Office Repository\//, ''); return ( <a key={index} href={`http://192.168.2.5:3006/files/${encodeURIComponent(relativePath)}`} target="_blank" rel="noopener noreferrer" className="suggestion-item" > {file.name} </a> ); }) ) : ( <div className="no-suggestions">No matches found</div> )} </div> )} </div> </div> </div> {/* Main Content */} <div className="main-content"> <div className="search-container"> <form onSubmit={handleSubmit} className="search-form"> <input type="text" placeholder="Enter search term" value={searchTerm} onChange={(e) => setSearchTerm(e.target.value)} className="search-input" /> <input type="text" placeholder="Exclude words (optional)" value={excludeWords} onChange={(e) => setExcludeWords(e.target.value)} className="search-input" /> <div className="search-type"> <label> <input type="radio" value="issues" checked={searchType === 'issues'} onChange={handleSearchTypeChange} /> Issues </label> <label> <input type="radio" value="judgments" checked={searchType === 'judgments'} onChange={handleSearchTypeChange} /> Judgments </label> </div> <button type="submit" disabled={isLoading} className="search-button" > {isLoading ? 'Searching...' : 'Search'} </button> </form> {error && <div className="error-message">{error}</div>} <div className="results-section"> {isLoading ? ( <div className="loading">Loading...</div> ) : searchResults.length > 0 ? ( <> <div className="results-count"> Found {searchResults.length} documents </div> <div className="results-list"> {searchResults.map((result, index) => ( <ResultCard key={index} result={result} onAddToLibrary={handleAddToLibrary} cardNumber={index + 1} /> ))} </div> </> ) : ( <div className="no-results">No results found</div> )} </div> </div> </div> </div> ); }; export default Home;Key Changes:
- Radio Buttons: Added a
divwith two radio buttons for "Issues" and "Judgments". searchTypeState: A new state variablesearchTypeis introduced to keep track of the selected search option.- Handling Changes: The
handleSearchTypeChangefunction updates thesearchTypestate. - Fetch Request Update: The
searchTypeis sent as part of the query parameters in the fetch request insidehandleSubmit.
CSS Considerations
Make sure your CSS styles allow for correct display and spacing of the new elements (radio buttons, etc.). You may want to add some classes to style the radio buttons and labels for better visual alignment.
This implementation should work seamlessly within your application, allowing users to select between "Issues" and "Judgments" as they search for documents.
- Radio Buttons: Added a
- User
import os import csv import pandas as pd from playwright.sync_api import sync_playwright
APPLICATION_PAGE_URL = "https://iprsearch.ipindia.gov.in/PublicSearch/PublicationSearch/ApplicationStatus" PDF_DIRECTORY = "applications1_pdfs"
def scrape_application_data(application_number): """ Scrape application data for a single application number. """ with sync_playwright() as p: browser = p.chromium.launch(headless=False)
page = browser.new_page()try: print(f"Visiting the website for application number: {application_number}") page.goto(APPLICATION_PAGE_URL, wait_until="domcontentloaded", timeout=60000) page.wait_for_selector('input[name="ApplicationNumber"]', timeout=60000) page.fill('input[name="ApplicationNumber"]', application_number) print("Please enter the CAPTCHA manually in the browser and submit the form.") page.wait_for_url("https://iprsearch.ipindia.gov.in/PatentSearch/PatentSearch/ViewApplicationStatus", timeout=90000) if page.locator("text=Sorry, we can't load the resource").is_visible(): print(f"Failed to load data for application number {application_number}: Sorry, we can't load the resource") return {"Application Number": application_number, "Status": "Resource Not Loaded"} print(f"Successfully loaded application status page for application number {application_number}.") # Extract desired fields application_type = safe_extract_text(page.locator('td:has-text("APPLICATION TYPE") + td')) date_of_filing = safe_extract_text(page.locator('td:has-text("DATE OF FILING") + td')) applicant_name = safe_extract_text(page.locator('td:has-text("APPLICANT NAME") + td strong')) title_of_invention = safe_extract_text(page.locator('td:has-text("TITLE OF INVENTION") + td')) field_of_invention = safe_extract_text(page.locator('td:has-text("FIELD OF INVENTION") + td')) email = safe_extract_text(page.locator('td:has-text("E-MAIL (As Per Record)") + td')) additional_email = safe_extract_text(page.locator('td:has-text("ADDITIONAL-EMAIL (As Per Record)") + td')) updated_email = safe_extract_text(page.locator('td:has-text("E-MAIL (UPDATED Online)") + td')) pct_international_application_number = safe_extract_text(page.locator('td:has-text("PCT INTERNATIONAL APPLICATION NUMBER") + td')) pct_international_filing_date = safe_extract_text(page.locator('td:has-text("PCT INTERNATIONAL FILING DATE") + td')) priority_date = safe_extract_text(page.locator('td:has-text("PRIORITY DATE") + td')) request_for_examination_date = safe_extract_text(page.locator('td:has-text("REQUEST FOR EXAMINATION DATE") + td')) publication_date = safe_extract_text(page.locator('td:has-text("PUBLICATION DATE (U/S 11A)") + td')) first_exam_report_date = safe_extract_text(page.locator('td:has-text("FIRST EXAMINATION REPORT DATE") + td')) date_of_certificate_issue = safe_extract_text(page.locator('td:has-text("Date Of Certificate Issue") + td')) post_grant_journal_date = safe_extract_text(page.locator('td:has-text("POST GRANT JOURNAL DATE") + td')) reply_to_fer_date = safe_extract_text(page.locator('td:has-text("REPLY TO FER DATE") + td')) # Extract Application Status application_status = safe_extract_text(page.locator('div.tab-pane.fade.active.in.Action.PatentDetails td:has-text("APPLICATION STATUS") + td h3 strong')) pdf_file_path = save_pdf(page, application_number) return { 'Application Number': application_number, 'Application Type': application_type, 'Date of Filing': date_of_filing, 'Applicant Name': applicant_name, 'Title of Invention': title_of_invention, 'Field of Invention': field_of_invention, 'E-Mail (As Per Record)': email, 'Additional Email': additional_email, 'E-Mail (Updated Online)': updated_email, 'PCT International Application Number': pct_international_application_number, 'PCT International Filing Date': pct_international_filing_date, 'Priority Date': priority_date, 'Request For Examination Date': request_for_examination_date, 'Publication Date': publication_date, 'First Examination Report Date': first_exam_report_date, 'Date Of Certificate Issue': date_of_certificate_issue, 'Post Grant Journal Date': post_grant_journal_date, 'Reply To FER Date': reply_to_fer_date, 'Application Status': application_status, } except Exception as e: print(f"Error during scraping for application number {application_number}: {e}") return {"Application Number": application_number, "Status": "Error", "Error Message": str(e)} finally: browser.close()def save_pdf(page, application_number): """ Save the content of the page as a PDF file. """ pdf_path = os.path.join(PDF_DIRECTORY, f"{application_number}.pdf") os.makedirs(PDF_DIRECTORY, exist_ok=True) page.pdf(path=pdf_path, format='A5', landscape=False)
print(f"PDF saved for application number {application_number} at {pdf_path}") return pdf_pathdef safe_extract_text(locator, default='N/A'): """ Safely extract text from a Playwright locator. """ try: return locator.inner_text().strip() except Exception: return default
def save_to_csv(data, filename): """ Append data to a CSV file. """ if data: file_exists = os.path.isfile(filename) with open(filename, mode='a', newline='', encoding='utf-8') as file: writer = csv.DictWriter(file, fieldnames=[ 'Application Number', 'Application Type', 'Date of Filing', 'Applicant Name', 'Title of Invention', 'Field of Invention', 'E-Mail (As Per Record)', 'Additional Email', 'E-Mail (Updated Online)', 'PCT International Application Number', 'PCT International Filing Date', 'Priority Date', 'Request For Examination Date', 'Publication Date','First Examination Report Date', 'Date Of Certificate Issue', 'Post Grant Journal Date', 'Reply To FER Date', 'Application Status']) if not file_exists: writer.writeheader() writer.writerow(data)
if name == "main": input_file = r"C:\Users\Administrator\Downloads\Application1.xlsx" #file path output_file = "data3.csv"
print("Reading application numbers from Excel file...") df = pd.read_excel(input_file) if 'Application number' not in df.columns: print("Column 'Application number' not found in Excel file.") exit() for idx, row in df.iterrows(): application_number = str(row['Application number']).strip() # Change this to your specific column name print(f"\nProcessing application number: {application_number}") application_data = scrape_application_data(application_number) print(f"Saving extracted application data for application number {application_number}...") save_to_csv(application_data, output_file) this is taking time when the dormat of page chnges like the other rows come they are not prsnt...so add somthing that if not prsnt return that column N/a AND MOVE FURTHERDONT TAKE TIME - Assistant
To modify your code to quickly handle scenarios where certain columns may not be present on the webpage, you can implement a timeout mechanism that allows the program to continue processing without indefinitely waiting for elements that may not exist.
The proposed approach involves the following adjustments:
- Adding a timeout for the extraction of each column's data.
- Using a helper function to handle the extraction with specified wait times.
Here's the updated code with these changes:
import os import csv import pandas as pd from playwright.sync_api import sync_playwright APPLICATION_PAGE_URL = "https://iprsearch.ipindia.gov.in/PublicSearch/PublicationSearch/ApplicationStatus" PDF_DIRECTORY = "applications1_pdfs" def scrape_application_data(application_number): """ Scrape application data for a single application number. """ with sync_playwright() as p: browser = p.chromium.launch(headless=False) page = browser.new_page() try: print(f"Visiting the website for application number: {application_number}") page.goto(APPLICATION_PAGE_URL, wait_until="domcontentloaded", timeout=60000) page.wait_for_selector('input[name="ApplicationNumber"]', timeout=60000) page.fill('input[name="ApplicationNumber"]', application_number) print("Please enter the CAPTCHA manually in the browser and submit the form.") page.wait_for_url("https://iprsearch.ipindia.gov.in/PatentSearch/PatentSearch/ViewApplicationStatus", timeout=90000) if page.locator("text=Sorry, we can't load the resource").is_visible(): print(f"Failed to load data for application number {application_number}: Sorry, we can't load the resource") return {"Application Number": application_number, "Status": "Resource Not Loaded"} print(f"Successfully loaded application status page for application number {application_number}.") # Extract desired fields application_type = safe_extract_text_with_timeout(page.locator('td:has-text("APPLICATION TYPE") + td')) date_of_filing = safe_extract_text_with_timeout(page.locator('td:has-text("DATE OF FILING") + td')) applicant_name = safe_extract_text_with_timeout(page.locator('td:has-text("APPLICANT NAME") + td strong')) title_of_invention = safe_extract_text_with_timeout(page.locator('td:has-text("TITLE OF INVENTION") + td')) field_of_invention = safe_extract_text_with_timeout(page.locator('td:has-text("FIELD OF INVENTION") + td')) email = safe_extract_text_with_timeout(page.locator('td:has-text("E-MAIL (As Per Record)") + td')) additional_email = safe_extract_text_with_timeout(page.locator('td:has-text("ADDITIONAL-EMAIL (As Per Record)") + td')) updated_email = safe_extract_text_with_timeout(page.locator('td:has-text("E-MAIL (UPDATED Online)") + td')) pct_international_application_number = safe_extract_text_with_timeout(page.locator('td:has-text("PCT INTERNATIONAL APPLICATION NUMBER") + td')) pct_international_filing_date = safe_extract_text_with_timeout(page.locator('td:has-text("PCT INTERNATIONAL FILING DATE") + td')) priority_date = safe_extract_text_with_timeout(page.locator('td:has-text("PRIORITY DATE") + td')) request_for_examination_date = safe_extract_text_with_timeout(page.locator('td:has-text("REQUEST FOR EXAMINATION DATE") + td')) publication_date = safe_extract_text_with_timeout(page.locator('td:has-text("PUBLICATION DATE (U/S 11A)") + td')) first_exam_report_date = safe_extract_text_with_timeout(page.locator('td:has-text("FIRST EXAMINATION REPORT DATE") + td')) date_of_certificate_issue = safe_extract_text_with_timeout(page.locator('td:has-text("Date Of Certificate Issue") + td')) post_grant_journal_date = safe_extract_text_with_timeout(page.locator('td:has-text("POST GRANT JOURNAL DATE") + td')) reply_to_fer_date = safe_extract_text_with_timeout(page.locator('td:has-text("REPLY TO FER DATE") + td')) # Extract Application Status application_status = safe_extract_text_with_timeout(page.locator('div.tab-pane.fade.active.in.Action.PatentDetails td:has-text("APPLICATION STATUS") + td h3 strong')) pdf_file_path = save_pdf(page, application_number) return { 'Application Number': application_number, 'Application Type': application_type, 'Date of Filing': date_of_filing, 'Applicant Name': applicant_name, 'Title of Invention': title_of_invention, 'Field of Invention': field_of_invention, 'E-Mail (As Per Record)': email, 'Additional Email': additional_email, 'E-Mail (Updated Online)': updated_email, 'PCT International Application Number': pct_international_application_number, 'PCT International Filing Date': pct_international_filing_date, 'Priority Date': priority_date, 'Request For Examination Date': request_for_examination_date, 'Publication Date': publication_date, 'First Examination Report Date': first_exam_report_date, 'Date Of Certificate Issue': date_of_certificate_issue, 'Post Grant Journal Date': post_grant_journal_date, 'Reply To FER Date': reply_to_fer_date, 'Application Status': application_status, } except Exception as e: print(f"Error during scraping for application number {application_number}: {e}") return {"Application Number": application_number, "Status": "Error", "Error Message": str(e)} finally: browser.close() def save_pdf(page, application_number): """ Save the content of the page as a PDF file. """ pdf_path = os.path.join(PDF_DIRECTORY, f"{application_number}.pdf") os.makedirs(PDF_DIRECTORY, exist_ok=True) page.pdf(path=pdf_path, format='A5', landscape=False) print(f"PDF saved for application number {application_number} at {pdf_path}") return pdf_path def safe_extract_text_with_timeout(locator, default='N/A', timeout=5): """ Safely extract text from a Playwright locator with a timeout. """ try: locator.wait_for(timeout=timeout * 1000) return locator.inner_text().strip() except Exception: return default def save_to_csv(data, filename): """ Append data to a CSV file. """ if data: file_exists = os.path.isfile(filename) with open(filename, mode='a', newline='', encoding='utf-8') as file: writer = csv.DictWriter(file, fieldnames=[ 'Application Number', 'Application Type', 'Date of Filing', 'Applicant Name', 'Title of Invention', 'Field of Invention', 'E-Mail (As Per Record)', 'Additional Email', 'E-Mail (Updated Online)', 'PCT International Application Number', 'PCT International Filing Date', 'Priority Date', 'Request For Examination Date', 'Publication Date', 'First Examination Report Date', 'Date Of Certificate Issue', 'Post Grant Journal Date', 'Reply To FER Date', 'Application Status']) if not file_exists: writer.writeheader() writer.writerow(data) if __name__ == "__main__": input_file = r"C:\Users\Administrator\Downloads\Application1.xlsx" # file path output_file = "data3.csv" print("Reading application numbers from Excel file...") df = pd.read_excel(input_file) if 'Application number' not in df.columns: print("Column 'Application number' not found in Excel file.") exit() for idx, row in df.iterrows(): application_number = str(row['Application number']).strip() # Change this to your specific column name print(f"\nProcessing application number: {application_number}") application_data = scrape_application_data(application_number) print(f"Saving extracted application data for application number {application_number}...") save_to_csv(application_data, output_file)Key Changes:
-
Timeout for Extraction: The
safe_extract_text_with_timeoutfunction now takes an additionaltimeoutparameter (in seconds). This function waits for a specified time for the element to be available and returns a default value ('N/A') if the element is not found within that time. -
Usage of New Function: All calls to
safe_extract_texthave been replaced withsafe_extract_text_with_timeout.
Note:
- Adjust the
timeoutparameter as necessary based on your requirements (e.g., increasing it if you're encountering frequent timeout errors). - This code now allows the scraper to skip over columns that take too long to load or do not exist, ensuring you can continue processing other application numbers without significant delays.