Tips for detecting HTML updates using Python requests

My goal is to monitor a page for updates while maintaining the same session and cookies, without sending a completely new request.

How can I determine if there are HTML updates within my current request? The page will redirect but keep the same URL.

This is the code I am currently using:

import requests

url = 'xxx'

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'
}

response = requests.get(url, headers=headers, allow_redirects=True, config={'keep_alive': True})


def get_status():
    html = response.text # this should be the current HTML, not the HTML when I made the initial request
    if x in html:
        status = "exists"
    else:
        status = "null"

return status


print(get_status())

UPDATE: I plan to use a while loop to execute this function every 5 seconds to check if the status equals "exists".

UPDATE2: Although I attempted to implement this using requests_html, I am not receiving all the necessary cookies:

import requests_html
from requests_html import HTMLSession

session = HTMLSession()
session.headers.update({'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'})
r = session.get('x')
r.html.render(reload=False)
print(r.cookies.get_dict())

Answer №1

Nevertheless, it is important to maintain the existing session and cookies in order to avoid sending a completely new request.

The solution lies in initiating a session as shown below:

s = requests.Session()
response = s.get("http://www.google.com")

By doing this, you will retain cookies and other relevant data throughout subsequent requests. Refer to the Sessions documentation for more detailed information.

If your main goal is to compare the current HTML response with the one from a previous request, simply store the initial response.text outside of your function and compare it with the new response.text.

In cases where the website generates content dynamically, this approach may not be effective. However, if you can identify a specific element in the DOM and compare it with the corresponding object from the prior request, this method should work effectively.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Experience choppy scrolling in Internet Explorer

Check out my click and drag scrolling Image Viewer here. While it functions perfectly in Firefox and Chrome, Internet Explorer is giving me some trouble. The movement seems jerky, especially when scrolling diagonally. It's like the scroll is sluggish ...

A guide on accessing a tuple within a dictionary

One thing that I am curious about is the data provided below, but in string format: obj = {"rnc": "44444044444", "cliente": "EMPRESA S.A.", "ncf": "1234567890123456789", "ncf_ref": "0987654321098765432", "tipo": "Factur ...

Enhancing cell contents by adding the titles of all hyperlinks

My goal is to update each row with the title of every link in the loop, but currently only one value is populating in the worksheet. I would like the output to be displayed as a list: Women's Walking Shoes Sock Sneakers Smart Watch for MenWomen .... ...

Calculate the number of days between two datetime fields in Python

I am currently working on a Python project using Flask where I need to calculate the difference between 2 DateField Objects and then perform calculations based on that difference. Here is the relevant code snippet from my views: @admin.route('/approv ...

Tips for adding up numbers from a file using a while loop

I'm currently working on developing a function that accepts a file name as input and then outputs the total sum of all the numbers found within that file. Here's my progress so far: def calculate_sum_from_file (filename): """ >>> ...

Problem encountered during the transfer of JSON data from PHP to JavaScript

Currently, I am working on a PHP file that extracts data from a database to display it on a chart using the Chart.js library. The chart is functioning properly, but I am facing an issue where I need to use the json_encode() function to pass the array value ...

Problems with aligning elements to both the left and right sides of the same line

Currently, I have two divs that I would like to float on the left and right sides. However, they seem to be sticking together and I can't seem to separate them.. HTML: <nav> <div id="nav_content"> <div id="home_ico ...

Choosing a checkbox by considering the checkbox value, especially in cases where the checkbox and its label are separate elements

My goal is to click on the checkbox located next to the label "Print Method 1". Take a look at how the element appears here Below is the HTML code for reference: <div class="texter"> <div class="checkbox"/> ...

Predicting outcomes using two variables through Linear Regression in a pandas dataframe

Although I'm not a programmer by trade, I am faced with the task of determining a relationship between two variables in an equation. Despite extensively searching through Google, I haven't been able to understand how to input my data into sklearn ...

Template for Joomla with a white stripe running along the right side

When I resize my browser to be half the width of my screen at www.thames.ikas.sk, a white stripe appears on the right side. Why is this happening? Why aren't my html and body elements taking up the full width of the browser? This issue doesn't oc ...

An error was encountered when attempting to reset_job as the JSON provided in the request body is incorrect - a map was anticipated

I encountered an issue while attempting to modify job settings using the CLI. When I triggered the reset_job method, an error occurred: Traceback (most recent call last): File "/home/vsts/work/1/s/S1.DataPlatform.DR/main.py", line 78, in <mo ...

What could be the reason why both the add and remove functions are unable to work simultaneously within a JavaScript function?

Hi there! I recently started diving into JavaScript and encountered a little hiccup. I've been working on a dice game where images change randomly whenever a button is clicked. The images transition from one to another, but I wanted to add a rolling ...

Activate the initial tab in JQuery UI accordion upon initialization

Hello, I have implemented a simple sidenav menu on my website. It consists of parent items and child items structured according to the h3 > div format as suggested by JQuery's documentation. My challenge now is to automatically open the "active" tab ...

Creating PNG images in a scripting language for web applications

Imagine giving your website user the ability to specify a radius size, let's say 5px, and in return receiving a PNG file with a circle of that radius. This question can be divided into two sections: In what language and using which technologies can ...

Flask: Extracting the raw body from a POST request with "application/x-www-form-urlencoded" content type

It has been discovered that Flask sets request.data as an empty string when the request's content type is application/x-www-form-urlencoded. In my case, I am utilizing a JSON body request and only wish to extract the json data or prompt Flask to do so ...

The recommended style guide for HTML documentation advises using spaces within the <code> tags

While reviewing the style guide for maintaining extensive documentation of an existing system using HTML for a client, I came across a particular rule that text within a code tag should be surrounded by spaces, like so: ..., the element<code> STATE ...

Issue with sticky positioning not functioning properly with overlapping scrolling effect

When the user scrolls, I want to create an overlapping effect using the 'sticky' position and assign a new background color to each div (section). However, despite setting 'top' to 0 for the 'sticky' position, the divs still s ...

Header that sticks to the top of a container as you scroll through it

Many questions arise regarding sticky elements in the DOM and the various libraries available to handle them, such as jquery.pin, sticky-kit, and more. However, the issue with most of these libraries is that they only function when the entire body is scro ...

Why isn't the page being redirected when attempting to use JavaScript with window.location and window.location.href?

Currently, I have implemented a login system on my website. The process involves triggering an ajax request to a designated PHP script upon the user clicking the submit button. This script is responsible for logging in the user and responding with the mess ...

Unable to find '/images/img-2.jpg' in the directory 'E:React eact-demosrc'

My code is giving me trouble when trying to add an image background-image: url('/images/img-2.jpg'); An error occurred during compilation. ./src/App.css (./node_modules/css-loader/dist/cjs.js??ref--5-oneOf-4-1!./node_modules/postcss-loader/src?? ...