Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 - Getting some strings from a HTTPrequest response

I'm having a hard time extracting data from a httprequest response.

Can somebody help me? Here's a part of my code:

import requests

r = requests.get('https://www.example.com', verify=True)
keyword = r.text.find('loginfield')
print (keyword)

>>> 42136

42136 value basically means that string 'loginfield' exists on the response.text. But how do I extract specific strings from it?

Like for example I want to extract these exact strings:

<title>Some title here</title>

or this one:

<div id='bla...' #continues extracting of strings until it stops where I want it to stop extracting.

Anybody got an idea on how should I approach this problem?

like image 428
Arthur Codova Avatar asked Jun 19 '26 09:06

Arthur Codova


1 Answers

You can use BeautifulSoup to parse HTML and get tags. Here's an example piece of code:

import requests
from bs4 import BeautifulSoup as BS
r = requests.get('https://www.example.com', verify=True)
soup = BS(r.text)
print(soup.find('title').text)

Should print:

Some title here

But depends on if it's the first title or not

like image 53
TerryA Avatar answered Jun 21 '26 00:06

TerryA



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!