0

I need to retrieve a JavaScript file content with Python. I thought that maybe requests would do the trick - perhaps it does but it's either not as straightforward as I thought or I'm doing something wrong.

import requests
s = requests.Session()
r = s.get(link)

The response I get is

<HTML><HEAD>
<TITLE>Invalid URL</TITLE>
</HEAD><BODY>
<H1>Invalid URL</H1>
The requested URL "&#91;no&#32;URL&#93;", is invalid.<p>
Reference&#32;&#35;9&#46;3e1151c8&#46;1484072058&#46;308a223a
</BODY></HTML>

What I need it's the same that what this code does in php,

$ch = curl_init();      
curl_setopt($ch, CURLOPT_URL, $link);
$html = curl_exec($ch);
4
  • "What I need it's the same that what this code does in php," What does this mean? Are you asking if the Python code would do the same thing as that PHP code? Commented Jan 10, 2017 at 18:24
  • I thought it would do the same, but it doesnt, or I'm doing something wrong. How do you get a javascript file content with requests or another python library? Commented Jan 10, 2017 at 18:30
  • I'm not able to answer your question, only to clarify it. Commented Jan 10, 2017 at 18:31
  • I had headers and other stuff initialized in the middle of the lines, that might be the problem because with a new requests object it is working, thanks! Commented Jan 10, 2017 at 18:52

1 Answer 1

1

You need to do a http get request

import urllib2
link = "http://whatever.com/your-file.js"
urllib2.urlopen(link).read()

The php code is using curl which is doing alot more than it looks

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.