Python 如何访问互联网?

Python把URL和lib组合成一个模块urllib在IDLE里面可以搜索:

Python入门学习笔记(网络爬虫)

可以看到urllib有四个模块:

urllib.request,urllib.error,urllib.parse,urllib.robotparser

Python入门学习笔记(网络爬虫)

测试使用urllib.request模块:

>>> import urllib.request
>>> response = urllib.request.urlopen("http://www.fishC.com")
>>> html = response.read()
>>> print(html)

打印出来的网页内容是二进制代码,对其进行转码得到整齐的网页代码:

>>> html = html.decode("utf-8")
>>> print(html)

 

相关文章: