首页 > 编程语言 > 详细

Python爬虫之BeautifulSoup和requests的使用

时间:2018-06-14 13:28:19      阅读:188      评论:0      收藏:0      [点我收藏+]

requests,Python HTTP 请求库,相当于 Android 的 Retrofit,它的功能包括 Keep-Alive 和连接池、Cookie 持久化、内容自动解压、HTTP 代理、SSL 认证、连接超时、Session 等很多特性,同时兼容 Python2 和 Python3。

 

第三方库的安装:

pip install urllib

pip install requests

 

 

小爬虫代码如下:

# -* - coding: UTF-8 -* -
#导入第三方库
import urllib
from bs4 import BeautifulSoup
import requests
url=‘https://www.phb123.com/junshi/lishi/9679_2.html‘
local="E:\\py\\imgs\\" #保存图片的文件夹
html_doc=requests.get(url).text
soup=BeautifulSoup(html_doc,‘lxml‘) #解析 html_doc
contens=soup.find_all(‘center‘)
x=1
for con in contens:
imgs=con.find_all(‘img‘) #获取center标签下的img标签
for img in imgs:
urllib.request.urlretrieve(img[‘src‘], local + ‘%s.jpg‘ % (x))
x =x+1



Python爬虫之BeautifulSoup和requests的使用

原文:https://www.cnblogs.com/ling-yu/p/9182277.html

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