python 爬虫(1)

来源:互联网 发布:电子数据取证产品 编辑:程序博客网 时间:2024/05/16 00:47

基础的爬行语句包括获取网页的html和解析,获取网页html内容主要用到urllib2库里的urlopen函数,返回值是个file-like object,记该对象为response,采用response.read()即可获得网页的html内容。然后利用BeautifulSoup函数解析网页即可。如下例:


import urllib2from bs4 import BeautifulSoupurl="https://www.baidu.com/"response=urllib2.urlopen(url)html=response.read()bs0=BeautifulSoup(html)print(bs0.script)     #打印<script>标签及其包含的内容


0 0
原创粉丝点击