Detect URL change in python


Linski

I need to detect the url change when the store is on sale, I am using the requests library with no luck, even the fire is still returning No deals on todayand the check value is still[u'http:', u'', u'www.dealwebsite.co', u'Electroshop']

Main store URLhttp://www.dealwebsite.com/coolshop

If there is a commodity deal on the main store then change the url to a redirect like redirecthttp://www.dealwebsite.com/coolshop/firesale

import requests

headers = {
'User-Agent': 'Mozilla\/5.0 (Windows NT 6.1; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/45.0.2454.101 Safari\/537.36'
}

select_shop = 'Electroshop'

url = 'http://www.dealwebsite.co/' + select_shop
r = requests.get(url, headers=headers, timeout=3)

check = r.url.split('/')

if len(check) != 5:
    print 'No deals on today'
    exit()
else:
    print 'Firesale Deals on NOW!'
mark

It looks like you can follow redirects . E.g:

requests.get(url, headers=headers, timeout=3, allows_redirect=True)
>>> r.url
'url'

>>> r.status_code
200

>>> r.history
[<Response [301]>] # means that there was a redirect on the way

In fact, you can just use the HEAD request to verify the behavior, only when the result is not required to be parsed (because the HEAD response body is empty).

>>> r = requests.head(url, headers=headers, timeout=3, allow_redirects=True)

>>> r.url
'..something...'

>>> r.history
[<Response [301]>] 

In theory, you could also block the redirect entirely, and check the response status.

>>> r = requests.get(url, headers=headers, timeout=3, allow_redirects=False)

>>> r.status_code
301

>>> r.history
[]

Now, a 301 could mean a redirect to wholesale or something - you don't know.

Update 1

For example, periscope.tv (it appears the OP has issues with a site like this):

>>> example = requests.get("https://periscope.tv/couchmode", allow_redirects=True)
>>> example.status_code
200
>>> example.history
[<Response [307]>]
>>> example.history[0].url
u'https://periscope.tv/couchmode'
>>> example.url
u'https://periscope.tv/w/aZwcYHNlcnZpY2V8MURYeHl6WUFaUWdLTerSfgniRKoRgIPbfxxlbAGofYQNBd8WZZTEelJ0KavI?mode=couch'

As you can see example.history[0].url tells you what is the URL that returns a 307 temporary redirect.

Related


Detect URL change in python

Linski I need to detect the url change when the store is on sale, I am using the requests library with no luck, even the fire is still returning No deals on todayand the check value is still[u'http:', u'', u'www.dealwebsite.co', u'Electroshop'] Main store URLh

Python detect url change?

username I'm creating a script that connects to wordpress and detects if the login is not correct, so I don't like the "if contains" approach, so I try to use this script: when you visit this url in a wordpress site: http://www.example.com/blog/wp-admin/profil

Python detect url change?

username I'm creating a script that connects to wordpress and detects if the login is not correct, so I don't like the "if contains" approach, so I try to use this script: when you visit this url in a wordpress site: http://www.example.com/blog/wp-admin/profil

Detect URL change in SFSafariViewController

username I have a SFSafariViewController that opens when the user clicks a link in my app. I need to detect when the URL changes so that when the URL changes, the app shows an alert. How can we detect URL change in SFSafariViewController? give You can't do thi

Detect URL change in SFSafariViewController

username I have a SFSafariViewController that opens when the user clicks a link in my app. I need to detect when the URL changes so that when the URL changes, the app shows an alert. How can we detect URL change in SFSafariViewController? give You can't do thi

Detect URL change in SFSafariViewController

username I have a SFSafariViewController that opens when the user clicks a link in my app. I need to detect when the URL changes so that when the URL changes, the app shows an alert. How can we detect URL change in SFSafariViewController? give You can't do thi

Detect URL change in SFSafariViewController

username I have a SFSafariViewController that opens when the user clicks a link in my app. I need to detect when the URL changes so that when the URL changes, the app shows an alert. How can we detect URL change in SFSafariViewController? give You can't do thi

Detect URL change in SFSafariViewController

username I have a SFSafariViewController that opens when the user clicks a link in my app. I need to detect when the URL changes so that when the URL changes, the app shows an alert. How can we detect URL change in SFSafariViewController? give You can't do thi

SwiftUI WKWebView detect url change

Alparslan Selcuk Develioglu I am an agile learner. I'm using SwiftUI and it's a struct where I have to implement WKWebView and the url changes dynamically. I have to catch these changing urls, but the solutions I've tried don't work. For example: https://stack

Detect URL change in JavaFX WebView

username In JavaFX, WebViewI am struggling to detect URL changes. I have this method in my class: public Object urlchange() { engine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() { @Override public void changed(Obs

How to detect URL change in SPA

Deepak Ingle Notice The solution should use pure Javascript - no external libraries and frameworks required. In a SPA, everything is routed using a routing mechanism. I only want to listen to even numbers when any part of the url changes (not just the hash. An

JS: Detect URL change in SPA

wasddd_ I would like to hear about path changes in my unmaintained SPA. I found a solution here : https://stackoverflow.com/a/44819548/7042552 However, this still seems "hacky" to me - but my implementation still looks like this: let url = window.location.href

SwiftUI WKWebView detect url change

Alparslan Selcuk Develioglu I am an agile learner. I'm using SwiftUI and it's a struct where I have to implement WKWebView and the url changes dynamically. I have to catch these changing urls, but the solutions I've tried don't work. For example: https://stack

How to detect URL change in SPA

Deepak Ingle Notice The solution should use pure Javascript - no external libraries and frameworks required. In a SPA, everything is routed using a routing mechanism. I only want to listen to even numbers when any part of the url changes (not just the hash. An

Detect URL change in JavaFX WebView

username In JavaFX, WebViewI am struggling to detect URL changes. I have this method in my class: public Object urlchange() { engine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() { @Override public void changed(Obs

Detect URL change in Webview android

Abishek Maharajpe I am using android webview and load url. In the webview, there is some click event, and on click, the UI in the webview changes. I don't get any callback in ShouldOverrideUrlLoading(...) . Is there a way to detect changes inside webview in an

How to detect URL change in SPA

Deepak Ingle notes The solution should use pure Javascript - no external libraries and frameworks required. In a SPA, everything is routed using a routing mechanism. I just want to listen once when any part of the url changes (not just the hash. Any changes I

JS: Detect URL change in SPA

wasddd_ I would like to hear about path changes in my unmaintained SPA. I found a solution here : https://stackoverflow.com/a/44819548/7042552 However, this still seems "hacky" to me - but my implementation still looks like this: let url = window.location.href

Detect URL change in WPF WebBrowser

Marcin Limanski Using WebBrowser in WPF, how to check for changes in URL? Can I be fired when the conditions are met? Below, I have a button event that will be set App.browserLinkCheckto the target URL, and open the WebBrowser instance. private void btNextWelc

How to detect URL change in JQuery?

username What I'm trying to do is refresh localStoragewhen the user moves to a different page . For example, let's say I'm currently at http://foo.com/accounts/mypage . I want to refresh when the user moves to the url http://foo.com/album .localStorage Here is

Detect URL change in JavaFX WebView

username In JavaFX, WebViewI am struggling to detect URL changes. I have this method in my class: public Object urlchange() { engine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() { @Override public void changed(Obs

SwiftUI WKWebView detect url change

Alparslan Selcuk Develioglu I am an agile learner. I'm using SwiftUI and it's a struct where I have to implement WKWebView and the url changes dynamically. I have to catch these changing urls, but the solutions I've tried don't work. For example: https://stack

How to detect URL change in SPA

Deepak Ingle Notice The solution should use pure Javascript - no external libraries and frameworks required. In a SPA, everything is routed using a routing mechanism. I only want to listen to even numbers when any part of the url changes (not just the hash. An

How to detect URL change in SPA

Deepak Ingle Notice The solution should use pure Javascript - no external libraries and frameworks required. In a SPA, everything is routed using a routing mechanism. I only want to listen to even numbers when any part of the url changes (not just the hash. An

JS: Detect URL change in SPA

wasddd_ I would like to hear about path changes in my unmaintained SPA. I found a solution here : https://stackoverflow.com/a/44819548/7042552 However, this still seems "hacky" to me - but my implementation still looks like this: let url = window.location.href

Detect URL change in Webview android

Abishek Maharajpe I am using android webview and load url. In the webview, there is some click event, and on click, the UI in the webview changes. I don't get any callback in ShouldOverrideUrlLoading(...) . Is there a way to detect changes inside webview in an

Detect URL change in WPF WebBrowser

Marcin Limanski Using WebBrowser in WPF, how to check for changes in URL? Can I be fired when the conditions are met? Below, I have a button event that will be set App.browserLinkCheckto the target URL, and open the WebBrowser instance. private void btNextWelc

Detect value change in Python loop

and This is the pattern I use often: last_value = None while <some_condition>: <get current_value from somewhere> if last_value != current_value: <do something> last_value = current_value An example of an application is printing a title i

Detect value change in Python loop

and This is the pattern I use often: last_value = None while <some_condition>: <get current_value from somewhere> if last_value != current_value: <do something> last_value = current_value An example of an application is printing a header