随便爬了一个网站,因为算法的原因容易堆栈溢出,小白一只,求指教
import requests
from bs4 import BeautifulSoup
import os
import sys
sys. setrecursionlimit ( 100000 )
i= 0
def getPicture ( url, url2) :
global i
try :
i = i+ 1
html = requests. get ( url)
bs = BeautifulSoup ( html. content, 'lxml' )
img = bs. find ( 'div' , { 'class' : 'pic-main' } ) . find ( 'img' ) [ 'src' ]
except AttributeError:
i= 1
html = requests. get ( url2)
bs = BeautifulSoup ( html. content, 'lxml' )
img = bs. find ( 'div' , { 'class' : 'pic-main' } ) . find ( 'img' ) [ 'src' ]
picname = bs. find ( 'div' , { 'class' : 'pic-main' } ) . find ( 'img' ) [ 'alt' ]
url = 'http://www.656g.com' + bs. find ( 'div' , { 'class' : 'pic-main' } ) . find ( 'a' ) [ 'href' ]
url2 = 'http://www.656g.com' + bs. find ( 'div' , { 'class' : 'pic-a' } ) . find ( 'a' , { 'class' : 'pic-next' } ) [ 'href' ]
img = requests. get ( img) . content
if os. path. exists ( '/Users/seantia/Desktop/webscrap/mm131/image/{}' . format ( picname) ) == False:
os. mkdir ( '/Users/seantia/Desktop/webscrap/mm131/image/{}' . format ( picname) )
os. chdir ( '/Users/seantia/Desktop/webscrap/mm131/image/{}' . format ( picname) )
with open ( '{}{}' . format ( picname, i) + '.jpg' , 'wb' ) as f:
f. write ( img)
print ( url)
getPicture ( url, url2)
getPicture ( 'http://www.656g.com/meinv/xinggan/16148_2.html' , ' ' )