SoFunction
Updated on 2024-12-19

Python crawler to achieve crawling Jingdong cell phone page images (example code)

Examples are shown below:

__author__ = 'Fred Zhao'
 
import requests
from bs4 import BeautifulSoup
import os
from  import urlretrieve
 
class Picture():
 
 def __init__(self):
   = {'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 = '/?cat=9987,653,655&page='
  self.base_path = (__file__)
 
 def makedir(self, name):
  path = (self.base_path, name)
  isExist = (path)
  if not isExist:
   (path)
   print("File has been created.")
  else:
   print('OK!The file is existed. You do not need create a new one.')
  (path)
 
 def request(self, url):
  r = (url, headers=)
  return r
 
 def get_img(self, page):
  r = (self.base_url + str(page))
  plist = BeautifulSoup(, 'lxml').find('div', id='plist')
  item = plist.find_all('li', class_='gl-item')
  print(len(item))
  ('pictures')
  num = 0
  for i in item:
   num += 1
   imglist = ('div', class_='p-img')
   print(num)
   img = ('img')
   print('This is %s picture' %num)
   if ('src'):
    url = 'https:' + ('src')
    fileName = ('src').split('/')[-1]
    urlretrieve(url, filename=fileName)
 
   elif ('data-lazy-img'):
    url = 'https:' + ('data-lazy-img')
    fileName = ('data-lazy-img').split('/')[-1]
    urlretrieve(url, filename=fileName)
 
 
 
if __name__ == '__main__':
 picture = Picture()
 for i in range(2): # Control the number of pages crawled
  picture.get_img(i+1)

Above this Python crawler to achieve crawling Jingdong cell phone page images (example code) is all I have shared with you, I hope to be able to give you a reference, and I hope you will support me more.