How can I use NSURLConnection in Keyboard Maestro?

My goal is getting a webpage content without opening a browser.

I’ve studied the issue already and tried this method: Create Web Archives (Download Web Page) from a List of URLs which saves the content in a file.

But I just want to save it in a text variable.

Recently I found 2 other possibilities: dataWithContentsOfURL and NSURLConnection from here:

Guys say that NSURLConnection works fine for this job, but I can’t imagine how I can execute this type of code in KM:

[NSURLConnection sendAsynchronousRequest: [NSURLRequest requestWithURL: url]
queue: [NSOperationQueue mainQueue]
completionHandler: ^( NSURLResponse *response, NSData *data, NSError *error ) {
// Process response in this log
}];

I’m afraid I can’t help you with NSURLConnection, but if you’re running KM8, you can now use the new Get a URL action to save webpage content directly to a variable.

3 Likes

Really? Wow, I need to check it asap. If this action gets the webpage content, it’s so cool!
Thank you!

1 Like

Thank you a lot! This new “Get a URL” feature is so nice! I am so happy I’ve bought KM8 :smiley:

2 Likes

Could you please help with one more question?

I need to extract several words from html content, but can’t figure out the end of line markers using regex, it doesn’t work. Example of part of content:

<div class="cfm-player-profile Teamcolors">
	<div class="cfm-player-team">
					<img src="30.png">
				</div>
	<div class="cfm-player-img">
		<img src="8045.png" onerror="if (this.src != '/0.png') {this.src='0.png'}" width="120px" height="120px"  /> 
	</div>
	<div class="cfm-player-bio">
		 <h1 class="cfm-player-name">Player  <span class="pull-right"> #22 </span></h1>
	</div>
	<div class="cfm-player-info">
		<p> <strong>Ovr:</strong> 90   
			<strong>Height:</strong> 6&#039;3&#039;&#039; 
			<strong>Weight:</strong> 222    
			<strong>Age:</strong> 23   
		</p>
		<p>     </p>
		 
	</div>
</div>

<div class="panel panel-default">
	  <div class="panel-heading"><span class="salary-info"><strong>.</strong>  </span>
	  <span class="pull-right">	<strong> </strong>   </span>
	  </div>
		  <div class="panel-body">
			 
			 <div class="cfm-player-attributes"> 
																<div class="cfm-player-attributes-item">
						<p class="cfm-player-attributes-item-header">SPD</p>
						<p class="cfm-player-attributes-item-level">89</p> 
						<div class="cfm-player-attributes-item-progress">
						  <div class="cfm-player-attributes-item-progress-bar progress-bar-success " role="progressbar" aria-valuenow="89" aria-valuemin="0" aria-valuemax="100" style="width: 89%;"></div>
						</div>
					</div> 
					<div class="cfm-player-attributes-item">
						<p class="cfm-player-attributes-item-header">ACC</p>
						<p class="cfm-player-attributes-item-level">87</p>  
						<div class="cfm-player-attributes-item-progress">
						  <div class="cfm-player-attributes-item-progress-bar progress-bar-success " role="progressbar" aria-valuenow="87" aria-valuemin="0" aria-valuemax="100" style="width: 87%;"></div>

When I am trying to search some words, it takes too much time to filter the text since it’s huge, so I decided to to divide this text into several parts or extract specific number of lines using expression (?m)(.\n){8}(.\n){3}(.*\n){5}
but this doesn’t work, it can’t find these matches. How can I extract specific line specifying it’s number?

I wouldn't be so quick to give up on using regex to extract words from the HTML. This quick-and-dirty sample macro I just put together:

Extract Text from HTML.kmmacros (5.8 KB)
49 AM

produces these results on your sample text:

5

I imagine you would see similar results even on much longer HTML content, so I would be inclined to give this method a shot first. If you know you only want words from specific lines, I would suggest refining the regex or even using multiple ones to extract the specific text you're looking for based on context, rather than position. If you're not sure how to do so feel free to post again, but please try to be as specific as you can about what it is you're trying to accomplish (for example, what words precisely are you trying to extract? How do you want to look for them? What does the full text that you're searching this data for look like? etc.)

1 Like

I got all you mean. Thank you for such an extensive answer, seems that’s exactly what I was looking for!

1 Like

Hey Dmitry,

Gabe has shown you the easiest method of scraping code from a remote web page, but there are a few more options to be aware of:

Methods of Retrieving the Source Code of a Web Page

-Chris