from urllib.request import urlopen
from bs4 import BeautifulSoup as bf
发出请求,获取html(获取到的是字节,需要转换)
 
html=urlopen("http://www.baidu.com")
 
 
用beautifulsoup将获取的内容转换为结构化内容
 
obj=bf(html.read(),‘html.parser‘)
 
 
find_all方法可以找出所有的对应标签
 
logo_pic_info=obj.findall(‘img‘,class="index-logo-src")
 
 
 
logo_url="http:"+logo_pic_info[1][‘src‘]
 
 
 
用find_all获取的标签是一个数组,可以用数组的访问方法访问。
原文:https://blog.51cto.com/15052789/2563577