爬虫框架 Beautiful Soup 4 使用心得

Beautiful Soup 4.4.0 

1. 参考

官方文档:http://beautifulsoup.readthedocs.io/zh_CN/latest/


2. 解压后

目录结构如下:





吐舌头小技巧:

如果WinZIP解压不了tar.gz,则可以先传到Linux机器上使用 tar 命令解压,然后回传到windows机器上来。

3. 注意事项

1) 电脑先要安装Python,这个可以搜索一下,下载相应的版本!我下载的是3.3.1
2) Python 3.3.1 环境下,build bs4.4会不成功,build bs4.3是成功的!

4. 附录

Windows安装:http://blog.csdn.net/jenyzhang/article/details/45828481

Python编辑器:Pycharm community Edition IDE


5. 初步尝试

Take action!



6. 爬取子页面数据的Python代码

我又编辑了一下文章,因为已经完成了完整的代码,爬取到了需要的子页面数据,保存到了excel中。开心!又会了一门东西!

链接:http://pan.baidu.com/s/1jHFfdWy


7. 使用到的 Python 语法

7.1 函数定义

def funtion1(param1, param2)

..omit...

return var1;

7.2 流程语句

7.2.1 If..else
a. 判断对象/字符串是否为空

if (var1 is None):
	print("var1 is None")
else:
	#print("var1 is not None");

b. 判断列表是否为空

if len(list1) == 0:
	print("list is empty");
else:
	print("list is not empty");

7.2.2 Try..except
异常捕捉

try:
	print("this is try clause");
except:
	print("handling clause in exception");
finally:
	print("finally clause");

7.2.3 For..in

迭代循环

for link in soup.find_all("a[href]"):
	href = link["href"];           # also used as link.get("href");
	print("href is %s" % (href));

7.3 BeautifulSoup 语法

7.3.1 CSS选择器

soup.select("a[href]")   --》 选择带有href属性的<a> tag.

soup.select('div[title*="关键字"]')  --》选择 title属性含有 “关键字“ 的<div> tag.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值