码迷,mamicode.com
首页 > 编程语言 > 详细

Python 爬虫

时间:2017-07-24 10:03:23      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:安装   cti   python 爬虫   输入   网站   latest   from   function   import   

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 爬虫

标签:安装   cti   python 爬虫   输入   网站   latest   from   function   import   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!