1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import requests from bs4 import BeautifulSoup headers = {'User-Agent':"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"} all_url = 'http://www.mzitu.com/all' start_html = requests.get(all_url, headers=headers) Soup = BeautifulSoup(start_html.text, 'lxml')
all_a = Soup.find('div', class_='all').find_all('a') for a in all_a: title = a.get_text() href = a.get('href') html = requests.get(href, headers=headers) html_Soup = BeautifulSoup(html.text, 'lxml') max_span = html_Soup.findAll('span')[10].get_text() for page in range(1, int(max_span) + 1): page_url = href + '/' + str(page) img_html = requests.get(page_url, headers=headers) img_Soup = BeautifulSoup(img_html.text, 'lxml') img_url = img_Soup.find('div', class_='main-image').find('img')['src'] name = img_url[-9:-4] img = requests.get(img_url, headers=headers) f = open(name + '.jpg', 'ab') f.write(img.content) f.close()
|