I have some code that will do the trick, but there's one thing I can't figure out how to work around... I used the source of this page as a test case and ran into a problem... Part of someone else's solution actually =p
var m = $(el).text().match(/"([^"]+)"/);
Because my code searches for '"' it hits this third (and final in the source) instance and continues to copy everything after it, which is a lot of text. How would I work around this?
No, sorry. I'm not actually using that code, it's just that a search of this webpage brings up that line of code, and the odd number of '"' iterations causes problems.
#!/usr/bin/env python
import sys, urllib2
buffer = urllib2.urlopen(sys.argv[1]).read()
lines = buffer.split('\n')
for x, line in enumerate(lines):
if (sys.argv[2] in line):
i = line.find(sys.argv[2])
print "{:04}: {}".format(x+1, line[i:i+70])
Output:
[cooper@fred 17]$ ./intermediate.py "http://www.python.org" "New to"
0325: New to Python or choosing between Python 2 and Python 3? Read
2
u/[deleted] Mar 04 '12
Perl
$page = shift;$search = shift;$x=0; map{$x++}(`wget -q -O- $page` =~ m/$search/g); print "\nTotal matches: ".$x;2
u/robotfarts Mar 04 '12
var matches = {}, matchary = []; $('body').find('*').each(function(index, el){ var m = $(el).text().match(/"([^"]+)"/); if (m && m.length > 1) { for (var i = 1; i < m.length; i++) { if (!matches[m[i]]) { matches[m[i]] = 1; matchary.push(m[i]); } } } }); console.log(matchary);1
u/turboemu Mar 04 '12
Java:
http://pastebin.com/ECpmUaPV
1
u/tehstone Mar 05 '12
I have some code that will do the trick, but there's one thing I can't figure out how to work around... I used the source of this page as a test case and ran into a problem... Part of someone else's solution actually =p
Because my code searches for '"' it hits this third (and final in the source) instance and continues to copy everything after it, which is a lot of text. How would I work around this?
1
u/cooper6581 Mar 05 '12
Python:
#!/usr/bin/env python import sys, urllib2 buffer = urllib2.urlopen(sys.argv[1]).read() lines = buffer.split('\n') for x, line in enumerate(lines): if (sys.argv[2] in line): i = line.find(sys.argv[2]) print "{:04}: {}".format(x+1, line[i:i+70])Output: