Python爬虫之BeautifulSoup和requests的使用
生活随笔
收集整理的這篇文章主要介紹了
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
轉載于:https://www.cnblogs.com/ling-yu/p/9182277.html
總結
以上是生活随笔為你收集整理的Python爬虫之BeautifulSoup和requests的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 源哥每日一题第十七弹 poj 1568
- 下一篇: Intellij IDEA常用配置详解