@@ -31,6 +31,9 @@ def search(
3131 Parameter (name = ["-o" , "--output" ], help = "Output file path (.json or .csv format)" ),
3232 ] = None ,
3333 headless : bool | None = None ,
34+ user_agent : Annotated [
35+ str | None , Parameter (help = "Custom user agent string for requests" )
36+ ] = None ,
3437 email : Annotated [str | None , Parameter (help = "LinkedIn email for authentication" )] = None ,
3538 password : Annotated [str | None , Parameter (help = "LinkedIn password for authentication" )] = None ,
3639 cookie : Annotated [
@@ -41,12 +44,14 @@ def search(
4144 try :
4245 config = _create_config (headless )
4346 credentials = _get_credentials (email , password , cookie )
47+ custom_user_agent = _get_user_agent (user_agent )
4448
4549 scraper = LinkedinSpider (
4650 email = credentials .get ("email" ),
4751 password = credentials .get ("password" ),
4852 li_at_cookie = credentials .get ("cookie" ),
4953 config = config ,
54+ user_agent = custom_user_agent ,
5055 )
5156
5257 results = scraper .search_profiles (query , max_results )
@@ -73,6 +78,9 @@ def profile(
7378 Parameter (name = ["-o" , "--output" ], help = "Output file path (.json or .csv format)" ),
7479 ] = None ,
7580 headless : bool | None = None ,
81+ user_agent : Annotated [
82+ str | None , Parameter (help = "Custom user agent string for requests" )
83+ ] = None ,
7684 email : Annotated [str | None , Parameter (help = "LinkedIn email for authentication" )] = None ,
7785 password : Annotated [str | None , Parameter (help = "LinkedIn password for authentication" )] = None ,
7886 cookie : Annotated [
@@ -83,12 +91,14 @@ def profile(
8391 try :
8492 config = _create_config (headless )
8593 credentials = _get_credentials (email , password , cookie )
94+ custom_user_agent = _get_user_agent (user_agent )
8695
8796 scraper = LinkedinSpider (
8897 email = credentials .get ("email" ),
8998 password = credentials .get ("password" ),
9099 li_at_cookie = credentials .get ("cookie" ),
91100 config = config ,
101+ user_agent = custom_user_agent ,
92102 )
93103
94104 result = scraper .scrape_profile (url )
@@ -119,6 +129,9 @@ def company(
119129 Parameter (name = ["-o" , "--output" ], help = "Output file path (.json or .csv format)" ),
120130 ] = None ,
121131 headless : bool | None = None ,
132+ user_agent : Annotated [
133+ str | None , Parameter (help = "Custom user agent string for requests" )
134+ ] = None ,
122135 email : Annotated [str | None , Parameter (help = "LinkedIn email for authentication" )] = None ,
123136 password : Annotated [str | None , Parameter (help = "LinkedIn password for authentication" )] = None ,
124137 cookie : Annotated [
@@ -129,12 +142,14 @@ def company(
129142 try :
130143 config = _create_config (headless )
131144 credentials = _get_credentials (email , password , cookie )
145+ custom_user_agent = _get_user_agent (user_agent )
132146
133147 scraper = LinkedinSpider (
134148 email = credentials .get ("email" ),
135149 password = credentials .get ("password" ),
136150 li_at_cookie = credentials .get ("cookie" ),
137151 config = config ,
152+ user_agent = custom_user_agent ,
138153 )
139154
140155 result = scraper .scrape_company (url )
@@ -167,6 +182,9 @@ def connections(
167182 Parameter (name = ["-o" , "--output" ], help = "Output file path (.json or .csv format)" ),
168183 ] = None ,
169184 headless : bool | None = None ,
185+ user_agent : Annotated [
186+ str | None , Parameter (help = "Custom user agent string for requests" )
187+ ] = None ,
170188 email : Annotated [str | None , Parameter (help = "LinkedIn email for authentication" )] = None ,
171189 password : Annotated [str | None , Parameter (help = "LinkedIn password for authentication" )] = None ,
172190 cookie : Annotated [
@@ -177,12 +195,14 @@ def connections(
177195 try :
178196 config = _create_config (headless )
179197 credentials = _get_credentials (email , password , cookie )
198+ custom_user_agent = _get_user_agent (user_agent )
180199
181200 scraper = LinkedinSpider (
182201 email = credentials .get ("email" ),
183202 password = credentials .get ("password" ),
184203 li_at_cookie = credentials .get ("cookie" ),
185204 config = config ,
205+ user_agent = custom_user_agent ,
186206 )
187207
188208 results = scraper .scrape_incoming_connections (max_results )
@@ -231,6 +251,9 @@ def search_posts(
231251 Parameter (name = ["-o" , "--output" ], help = "Output file path (.json or .csv format)" ),
232252 ] = None ,
233253 headless : bool | None = None ,
254+ user_agent : Annotated [
255+ str | None , Parameter (help = "Custom user agent string for requests" )
256+ ] = None ,
234257 email : Annotated [str | None , Parameter (help = "LinkedIn email for authentication" )] = None ,
235258 password : Annotated [str | None , Parameter (help = "LinkedIn password for authentication" )] = None ,
236259 cookie : Annotated [
@@ -241,12 +264,14 @@ def search_posts(
241264 try :
242265 config = _create_config (headless )
243266 credentials = _get_credentials (email , password , cookie )
267+ custom_user_agent = _get_user_agent (user_agent )
244268
245269 scraper = LinkedinSpider (
246270 email = credentials .get ("email" ),
247271 password = credentials .get ("password" ),
248272 li_at_cookie = credentials .get ("cookie" ),
249273 config = config ,
274+ user_agent = custom_user_agent ,
250275 )
251276
252277 print (f"Searching for posts with keywords: '{ keywords } '" )
@@ -285,20 +310,25 @@ def _create_config(headless: bool | None) -> ScraperConfig:
285310 return ScraperConfig (headless = headless )
286311
287312
313+ def _get_user_agent (user_agent : str | None ) -> str | None :
314+ """Get user agent from argument or environment variable."""
315+ return user_agent or os .getenv ("USER_AGENT" )
316+
317+
288318def _get_credentials (email : str | None , password : str | None , cookie : str | None ) -> dict :
289319 """Get authentication credentials from arguments or environment."""
290320 credentials = {
291321 "email" : email or os .getenv ("LINKEDIN_EMAIL" ),
292322 "password" : password or os .getenv ("LINKEDIN_PASSWORD" ),
293- "cookie" : cookie or os .getenv ("LINKEDIN_COOKIE " ),
323+ "cookie" : cookie or os .getenv ("cookie " ),
294324 }
295325
296326 if not any (credentials .values ()):
297327 raise ValueError (
298328 "Authentication required. Provide either:\n "
299329 "1. Email and password (--email, --password)\n "
300330 "2. LinkedIn cookie (--cookie)\n "
301- "3. Set environment variables: LINKEDIN_EMAIL, LINKEDIN_PASSWORD, or LINKEDIN_COOKIE "
331+ "3. Set environment variables: LINKEDIN_EMAIL, LINKEDIN_PASSWORD, or cookie "
302332 )
303333
304334 return credentials
0 commit comments