0
import requests

session = requests.Session()
session.headers.update({'Some headers'})
session.headers.update({'Some headers'})
session.headers.update({'Some headers'})
session.headers.update({'Some headers'})
session.headers.update({'Some headers'})

session.get('SomeWebsite').url

From this script I get an URL.

Just typing in the url wont work, because It doesnt have the same headers as In the response.

For Example If I go to www.example.com with the standard CF-RAY, I'll be redirected to A website www.example.com/No

If I go to www.example.com with the CF-RAY that I got from the response I'll be redirected to www.example.com/Yes

Bassicly In looking for something like this

session.get('SomeWebsite').OpenUrlInMyBrowser

Thanks alot! Stefan

S. Known
  • 195
  • 2
  • 2
  • 10
  • Can you explain clearly what you want? Did you desire to get same response as browser? If true you need to post your example url, cause dynamic loading – KC. Nov 08 '18 at 03:03
  • @kcorlidy If I execute the scipt given, than I get an html code of the response. I bet there's a url option, but then I wont go to the website with the same response headers – S. Known Nov 08 '18 at 07:48
  • You mean `session.get('SomeWebsite').cookies` ? They have difference between response header and request header. Or you mean random header? – KC. Nov 08 '18 at 08:36
  • @kcorlidy It isnt just the cookies – S. Known Nov 08 '18 at 09:27
  • @kcorlidy I updated the quistion, Did it help – S. Known Nov 08 '18 at 09:33
  • `CF_RAY= session.get('SomeWebsite').headers.get("CF-RAY")` you can try this – KC. Nov 08 '18 at 09:42
  • So you can try to finish the process with selenium and without `driver.close()`. – KC. Nov 08 '18 at 11:02
  • I already have al the headers. The only Thing I want is to open the response of my script in my browser – S. Known Nov 08 '18 at 11:04
  • Can you add my Skype,my name is same as now one, then i will help you. – KC. Nov 08 '18 at 11:21
  • @kcorlidy No sorry I cant. – S. Known Nov 08 '18 at 11:26
  • because i can not understand `open a response in browser`. You can create a new request by using some data on response. But if you really want to open a response in browser, which mean you desire to open a file, such as html file. – KC. Nov 08 '18 at 11:33
  • @kcorlidy I dont want to open it from a html file, because that one isnt hosted on the server, so the js and pictures etc. wont work – S. Known Nov 08 '18 at 11:48
  • I have try to add `CF-RAY` to header in selenium-chrome, but it does not allow(you question is special). You the better do all job with selenium, because in ordinarily people can access it correctly through browser. – KC. Nov 08 '18 at 12:20

1 Answers1

0

Not sure if this helps, I still don't completely understand your question.

You can display the headers of the response you get using:

session.get('SomeWebsite').headers

and include those in a new request. You could do this with a blanket:

response = session.get("http://www.example.com")
session2 = requests.Session()
session2.headers.update(response.headers)

but this doesn't make much sense because the defined headers for requests and responses are not the same. So you have to manually select the properties you want.

If you are not sure which properties you need this will tell you how to show all the info from a request.

This is how you open a URL in your browser

import webbrowser
webbrowser.open_new("http://www.example.com")
Pumpkin
  • 233
  • 1
  • 9
  • Did you ever see you request header with this code? Mr. You are doing something terrible – KC. Nov 08 '18 at 10:00
  • @kcorlidy What is your question? – Pumpkin Nov 08 '18 at 10:02
  • I think you should try your code and see your request content, which contains lots of it should not have, like `Status` – KC. Nov 08 '18 at 10:04
  • @kcorlidy I just want to open the response in my browser – S. Known Nov 08 '18 at 10:56
  • @S.Known I added how to open a URL in your browser. Is that what you are looking for? It does, however, not allow you to edit the header. – Pumpkin Nov 08 '18 at 11:45
  • @Pumpkin I dont know Does it open the website with the same headers as the response from the requests session? – S. Known Nov 08 '18 at 12:29
  • @S.Known Again: The headers are DIFFERENT for requests and responses, opening the website with the same headers as the response to an earlier request makes no sense. Also, as mentioned, this library does NOT allow changing the headers. So the answer is definitely "no". – Pumpkin Nov 08 '18 at 14:35