[Python/爬虫/]校花网

403次阅读
没有评论
[Python/爬虫/]校花网

最近一直在学习 爬虫,感觉模块很多种,比如比较偏向web的xpath(lxml),比较简单的bs4,还有正则的re, 今天爬取了一下 校花网的一个简单案例 代码如下:

#/usr/bin/env python # -*- coding:utf-8 -*- from urllib import request from bs4 import BeautifulSoup import re import requests import urllib

url = 'http://www.xiaohuar.com/list-1-{}.html' class SchoolFlower(object): def __init__(self,url): self.url = url self.header = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.109 Safari/537.36'} self.List = [] self.Connection()

def Connection(self): content = requests.get(self.url,headers=self.header) content.encoding = 'utf-8' soup = BeautifulSoup(content.content,'html.parser',from_encoding='gbk') return soup

def Getpicture(self): info = self.Connection() img = info.find_all('img',src=re.compile(r'/d/file/d+/w+.jpg')) x = 0 for i in img: print('正在下载弟{}张照片'.format(x)) f = open('%s.jpg' % i.get('alt'),'wb') f1 = requests.get('http://www.xiaohuar.com%s' % i.get('src')).content f.write(f1) x += 1 x = 0

for i in range(0,11): print('#########开始下载第{}页的图片##########'.format(i)) f = SchoolFlower(url.format(i)) f.Getpicture()

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:Python教程2022-10-24发表,共计1166字。
新手QQ群:570568346,欢迎进群讨论 Python51学习