第一个爬虫程序

使用的库

urllib、bs4——代码以下:

from urllib.request import urlopen
from bs4 import BeautifulSoup as bf

 

发出请求,获取html(获取到的是字节,须要转换)
 html

html=urlopen("http://www.baidu.com")
 
 数组

用beautifulsoup将获取的内容转换为结构化内容
 
obj=bf(html.read(),'html.parser')markdown

 
 
find_all方法能够找出全部的对应标签
 
logo_pic_info=obj.findall('img',class="index-logo-src")
 
 ide

最后注意观察获取到的url,缺啥补啥!

 
logo_url="http:"+logo_pic_info[1]['src']
 
 url

心得:

 
用find_all获取的标签是一个数组,能够用数组的访问方法访问。code

相关文章
相关标签/搜索