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='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



猜你喜欢

转载自www.cnblogs.com/ling-yu/p/9182277.html