Python How to open an URL and get source code at the same time?
<p>What I had try are as following:</p>

<p>1)</p>

<pre><code>response = urllib2.urlopen(url)
html = response.read()
</code></pre>

<p>In this way, I can't open the url in browser.</p>

<p>2)</p>

<pre><code>webbrowser.open(url)
</code></pre>

<p>In this way, I can't get source code of the url.</p>

<p>So, how can I open an URL and get source code at the same time?</p>

<p>Thanks for your help.</p>

<p>Have a look at BeautifulSoup: <a href="https://www.crummy.com/software/BeautifulSoup/" rel="nofollow">https://www.crummy.com/software/BeautifulSoup/</a></p>

<p>You can request a website and then read the HTML source code from it:</p>

<pre><code>import requests
from bs4 import BeautifulSoup

r = requests.get(YourURL)

soup = BeautifulSoup(r.content)
print soup.prettify()
</code></pre>

<p>If you want to read JavaScript, look into Headless Browsers.</p>

python