Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve text from response made from Requests python

I am trying to search inside the response of a request (I used Requests and Python). I get the response and check the type of it, which is UNICODE.

I want to retrieve a specific link which is located between two other strings. I have tried different ways found online such as the:

  • result = re.**search**('Currently: <a ', s)
  • url_file = response.**find**('Currently: <a ', beg=0, end=len(response))

Also tried to transform the UNICODE string to a normal string:

  • s = unicodedata.normalize(response, title).encode('ascii','ignore')

I get an error.

EDITED

For example:

This works:

    s = 'asdf=5;iwantthis123jasd'
    result = re.search('asdf=5;(.*)123jasd', s)
    print result.group(1)

This doesn't work (returns error):

    s = 'Currently: <a '
    result = re.search(r.text, s)
    print result.group(1)
like image 818
user1919 Avatar asked Apr 01 '26 21:04

user1919


1 Answers

You can access the raw text from the response object with the text attribute.

res = requests.get("http://google.com")
re.search('pattern', res.text)

Then, just use a regular expression to "search" or "match" the entire response.

like image 51
joaoricardo000 Avatar answered Apr 04 '26 11:04

joaoricardo000



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!