基础的爬行语句包括获取网页的html和解析,获取网页html内容主要用到urllib2库里的urlopen函数,返回值是个file-like object,记该对象为response,采用response.read()即可获得网页的html内容。然后利用BeautifulSoup函数解析网页即可。如下例:
import urllib2
from bs4 import BeautifulSoup
url="https://www.baidu.com/"
response=urllib2.urlopen(url)
html=response.read()
bs0=BeautifulSoup(html)
print(bs0.script) #打印<script>标签及其包含的内容