Get webpage contents with Python?
Because you're using Python 3.1, you need to use the new Python 3.1 APIs.
Try:
urllib.request.urlopen('http://www.python.org/')
Alternately, it looks like you're working from Python 2 examples. Write it in Python 2, then use the 2to3 tool to convert it. On Windows, 2to3.py is in \python31\tools\scripts. Can someone else point out where to find 2to3.py on other platforms?
Edit
These days, I write Python 2 and 3 compatible code by using six.
from six.moves import urllib
urllib.request.urlopen('http://www.python.org')
Assuming you have six installed, that runs on both Python 2 and Python 3.
How to get the contents of a webpage in a shell variable?
You can use wget
command to download the page and read it into a variable as:
content=$(wget google.com -q -O -)
echo $content
We use the -O
option of wget
which allows us to specify the name of the file into which wget
dumps the page contents. We specify -
to get the dump onto standard output and collect that into the variable content
. You can add the -q
quiet option to turn off's wget output.
You can use the curl command for this aswell as:
content=$(curl -L google.com)
echo $content
We need to use the -L
option as the page we are requesting might have moved. In which case we need to get the page from the new location. The -L
or --location
option helps us with this.
Retrieve the content of a web page
i think you need this
URL url = new URL("http://www.google.com/");
URLConnection con = url.openConnection();
InputStream in = con.getInputStream();
String encoding = null; // con.getContentEncoding(); *** WRONG: should use "con.getContentType()" instead but it returns something like "text/html; charset=UTF-8" so this value must be parsed to extract the actual encoding
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
System.out.println(body);
javascript: how to fetch the content of a web page
use a server-side proxy like a php-page that reads the desired page and then make ajax calls to that proxy through javascript :
var req = new XMLHttpRequest();
req.open('GET', 'proxy.php?url=http://www.google.com', false);
req.send(null);
if(req.status == 200) {
alert(req.responseText);
}
Related Topics
Using Memory Sanitizer with Libstdc++
How to Sort C++ Array in Asc and Desc Mode
What Is The Reason for Having Unreserved Identifiers as Built-In Macros in Gcc
In C++, Is a Function Automatically Virtual If It Overrides a Virtual Function
Std::Lower_Bound Slower for Std::Vector Than Std::Map::Find
Given a Start and End Point, and a Distance, Calculate a Point Along a Line
Calculate Md5 of a String in C++
C++ 'Strcpy' Gives a Warning (C4996)
How to Find the Minimum Value in a Map
Some Issues About Cygwin[Linux in Windows] (Socket,Thread,Other Programming and Shell Issues)
Why Does Gcc Not Seem to Have the Filesystem Standard Library
Communicate with Codesys Program on a Linux-Based Wago Pfc200 Plc
Catching Signals: Use a Member Function as Signal Handler
Cmake Error: "Add_Subdirectory Not Given a Binary Directory"