失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Python爬虫之BeautifulSoup和requests的使用

Python爬虫之BeautifulSoup和requests的使用

时间:2020-11-06 06:40:37

相关推荐

Python爬虫之BeautifulSoup和requests的使用

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='/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的使用》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。