python爬虫--BeautifulSoup的简单用法

BeautifulSoup的简单用法
#coding=utf-8
 
import urllib
import urllib2
import cookielib
from bs4 import BeautifulSoup
import re
 
url ="http://www.baidu.com"
 
try:
    request = urllib2.Request(url, data = None)
    response = urllib2.urlopen(request, timeout= 2)
except urllib2.HTTPError, e:
    print e.code
except urllib2.URLError, e:
    print e.reason
except:
    print "Error"
     
data = response.read()
soup = BeautifulSoup(data,"lxml")
 
for tag in soup.find_all('div',class_="qrcode-text"):
    for item in tag.children:
       print item
 
find_all('div',class_="qrcode-text")方法
 1、参数可以是name参数,如:’a’ ,’div’,[‘a’,’p’],re.compie(‘^b’),True等等
 2、参数可以是属性,比如:id=”link2”,href=re.compile(‘baidu’)等等
 3、参数还可以是text,用于匹配Tag的string,如text=”baidu”
 4、还可以混合起来使用,如上面程序所示
 5、tag.children:表示tag的所有子节点,返回的是类list结构
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值