圆月山庄资源网 Design By www.vgjia.com

实例如下所示:

__author__ = 'Fred Zhao'
 
import requests
from bs4 import BeautifulSoup
import os
from urllib.request import urlretrieve
 
class Picture():
 
 def __init__(self):
  self.headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36'}
  self.base_url = 'https://list.jd.com/list.html"File has been created.")
  else:
   print('OK!The file is existed. You do not need create a new one.')
  os.chdir(path)
 
 def request(self, url):
  r = requests.get(url, headers=self.headers)
  return r
 
 def get_img(self, page):
  r = self.request(self.base_url + str(page))
  plist = BeautifulSoup(r.text, 'lxml').find('div', id='plist')
  item = plist.find_all('li', class_='gl-item')
  print(len(item))
  self.makedir('pictures')
  num = 0
  for i in item:
   num += 1
   imglist = i.find('div', class_='p-img')
   print(num)
   img = imglist.find('img')
   print('This is %s picture' %num)
   if img.get('src'):
    url = 'https:' + img.get('src')
    fileName = img.get('src').split('/')[-1]
    urlretrieve(url, filename=fileName)
 
   elif img.get('data-lazy-img'):
    url = 'https:' + img.get('data-lazy-img')
    fileName = img.get('data-lazy-img').split('/')[-1]
    urlretrieve(url, filename=fileName)
 
 
 
if __name__ == '__main__':
 picture = Picture()
 for i in range(2): #控制爬取的页数
  picture.get_img(i+1)

以上这篇Python爬虫实现爬取京东手机页面的图片(实例代码)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

标签:
python,爬虫,爬取,京东,手机页面,图片

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com