首页 > 编程语言 > 详细

Python 爬虫

时间:2017-07-24 09:22:58      阅读:230      评论:0      收藏:0      [点我收藏+]

1.用Requests爬去你想要的爬取的网站

1
2
3
4
import requests
 
= requests.get(‘https://www.baidu.com‘)
print r.text # 打印网站源代码

注意:使用Requests前需要安装Requests库,安装方法,命令行输入:

1
pip install requests

2. 用Beautiful Soup解析网站源代码

安装:

1
pip install beautifulsoup4

解析:

1
2
3
4
5
6
7
8
from bs4 import BeautifulSoup # 引用BeautifulSoup库
import requests               # 引用Requests库
 
= requests.get(‘https://www.baidu.com‘)
html = r.text                 # 获取网站源代码
soup = BeautifulSoup(html)    #创建 beautifulsoup 对象
print soup.a                  # 获取网页的链接 (a标签)
# ...

PS:

Requests库的具体用法,见 http://docs.python-requests.org/zh_CN/latest/

BeautifulSoup库的具体用法,见 https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/

Python 爬虫

原文:http://www.cnblogs.com/siyu1915/p/7226951.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!