pzkpfw
pzkpfw

Reputation: 604

Some help understanding my own Python code

I'm starting to learn Python and I've written the following Python code (some of it omitted) and it works fine, but I'd like to understand it better. So I do the following:

html_doc = requests.get('[url here]')

Followed by:

if html_doc.status_code == 200:
    soup = BeautifulSoup(html_doc.text, 'html.parser')
    line = soup.find('a', class_="some_class")
    value = re.search('[regex]', str(line))
    print (value.group(0))

My questions are:

Upvotes: 0

Views: 65

Answers (2)

Iron Fist
Iron Fist

Reputation: 10951

Seems to me you are lacking some basic knowledge about Classes, Object and methods...etc, you need to read more about it here (for Python 2.7) and about requests module here.

Concerning what you asked, when you type html_doc = requests.get('url'), you are creating an instance of class requests.models.Response, you can check it by:

>>> type(html_doc)
<class 'requests.models.Response'>

Now, html_doc has methods, thus html_doc.text will return to you the server's response

Same goes for re module, each of its methods generates response object that are not simply int or string

Upvotes: 1

Łukasz Rogalski
Łukasz Rogalski

Reputation: 23203

requests.get() return value, as stated in docs, is Response object.

re.search() return value, as stated in docs, is MatchObject object.

Both objects are introduced, because they contain much more information than simply response bytes (e.g. HTTP status code, response headers etc.) or simple found string value (e.g. it includes positions of first and last matched characters).

For more information you'll have to study docs.

FYI, to check type of returned value you may use built-in type function:

response = requests.get('[url here]')
print type(response)  #  <class 'requests.models.Response'>

Upvotes: 4

Related Questions