Yesterday I made little update to my function get_html_source() that gets the content of a page. I did so because I found that my previous function didn't support HTTP POST. Now the code supports both HTTP GET and HTTP POST. It also returns the cookiejar along with the html content of the page. def get_html_source(url, referer = '', data = 0, cj = 0, retry_counter = 0): if retry_counter > 0: print 'Trying Again...' if retry_counter > 3: print 'Could not get source from url:', url return '', '' try: if cj: opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) else: opener = urllib2.build_opener() opener.addheaders = [('Referer', referer), ('Content-Type', 'application/x-www-form-urlencoded'), ('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/200...
Comments
--Hans
it really helpful.
thanks.
or any link for explanation will be helpful.
thanks.
Example:
>>> foo = [2, 5, 1, 8]
Regular slice (returns all)
>>> foo[:]
[2, 5, 1, 8]
Slice with stride (returns every nth)
>>> foo[::2]
[2, 1]
Stride works inversely as well
>>> foo[::-2]
[8, 5]
Hence it can be used to reverse a string (stride backwards using step length of one)
>>> 'foobar'[::-1]
'raboof'
There is another way to reverse as well. This can be done using "reversed" generator:
>>> ''.join([char for char in reversed('foobar')])
'raboof'
As the list comprehension returns a list I had to join it separately. Normally the [::-1] trick should work just fine. "reversed" may be useful in some special cases, however.
Check out http://www.python.org/doc/2.3.5/whatsnew/section-slices.html for more comprehensive documentation.
s = s[::2]
print s
#for this code it returns ac
#how
Does anyone know what i should use to extract string/substring from a string?
print $string = reverse("ABC")
>>>print s[:]
spam
>>>print s[::-1]
maps
>>>print s[0:len(s)]
spam
>>>print s[0:len(s):-1]
(prints nothing)
Any reason, why ??
s='abc'
print s[::-2]
returns 'ca' only...
You could do this:
oct(12)
that would return
'014' as a string
To enter an octal number literal, precede with a 0:
0122 will return 82 in base 10
to go from an octal string to an int:
int('014',8)
>>> sentence = "one two three four"
>>> ' '.join( [ word[::-1] for word in sentence.split() ] )[::-1]
'four three two one'
>>> sentence = "one two three four"
>>> ' '.join( [ word for word in sentence.split() ] [::-1])
'four three two one'
[::-1]
[:] <-- this gets all of the string
while [::(value)] skips or reverse the string. if it is negative, the skipping starts from the last character of the string depends on the number.
for example, you can put -2 instead of -1.
s = 'abcdef'
s = s[::-2]
print s
if counted base on the slice number, it would creat a group of three which are fe, dc and ba. the first character of each group (f,d,b) will appended to formed a value and stored it to the variable 's'.
revword = ''
for letter in reversed(word): revword += letter
print revword
Full code solutions with test data and output, described in clear English and tested on the same page
print (" ".join(str.split()[::-1])) would work for reversing string word by word :)
Thought to share it . :)