zoukankan      html  css  js  c++  java
  • python爬图 准备多线程

    #coding:utf-8
    #官方3.0版本已经把urllib2,urlparse等五个模块都并入了urllib中
    from urllib import request

    import re,urllib,random,time

    def getHtml(url):
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}
    page1=request.Request(url,headers=headers)
    page=request.urlopen(page1)
    # 获取网页内容
    html = page.read().decode('utf8')
    # print(html)
    return html

    def getHtml_href(html):
    #正则表达式
    reg = r'href="(.+?.html)"'
    href = re.compile(reg)
    #以列表的形式返回能匹配的子串
    hrefList = re.findall(href,html)
    href_list=[]
    for x in hrefList:
    #添加域名保存到列表里面
    href_list.append('https://www.113yq.com%s' % x)
    return href_list

    def getImg(html):
    # title_re=r'<h1 class="text-overflow">(.+?)</h1>'

    # title = re.findall(re.compile(title_re),html)[0]
    timename=time.strftime("%Y_%m_%d %H_%M_%S",time.localtime(time.time()))
    #<h1 class="text-overflow">女朋友粉嫩的鲍鱼[11P]</h1>
    #正则表达式
    reg = r'src="(.+?.jpg)" alt='
    imgre = re.compile(reg)
    #以列表的形式返回能匹配的子串
    imgList = re.findall(imgre,html.decode('utf-8'))
    x=0
    for imgurl in imgList:
    #把爬取到的资源保存到本地
    urllib.request.urlretrieve(imgurl,timename+'%s.jpg' % x)
    x+=1
    return imgList
    #输入你想要爬取的网站
    for x in range(2,46):
    url='https://www.113yq.com//pic/html28/index_%s.html.html' % x
    #url='https://www.113yq.com/pic/html28/index_3.html'
    html=getHtml(url)
    imgurl_list=getHtml_href(html)
    for x in imgurl_list:
    print(x)
    getImg(x)
    #html=getHtml("http://pic.yxdown.com/list/0_0_1.html")
    # print(getImg(html))

  • 相关阅读:
    oracle hint
    oracle资源
    数据迁移相关笔记
    csdn的blog可以直接导入内含图片的word文档吗?
    Windows Live Writer离线博客工具使用教程(适用于博客园、CSDN、51CTO等等博客)
    csdn的博客上传word图片
    怎样将word中的图片插入到CSDN博客中
    测试用Word2007发布博客文章
    用WORD2007发布博客文章
    Word2007发布博客
  • 原文地址:https://www.cnblogs.com/xiaohe520/p/10821679.html
Copyright © 2011-2022 走看看