zoukankan      html  css  js  c++  java
  • 从百度贴吧进行爬虫

    from urllib.request import urlopen
    from bs4 import BeautifulSoup
    from urllib.request import urlopen
    import re
    import random
    import os
    import urllib.request
    #把base_url随便改成别的贴吧的吧的首页都行,为了版权我还是不写上贴吧的具体地址了,用任意一个吧的首页来替换下面一行都行
    base_url = "https://tieba.baidu.com/!!!!!!!!!!!!!!!!!!!!!"
    num=5000
    for i in range(16,50):#爬取50页
           
           shumu=850+50*i
           url=base_url+str(shumu)
           print (url)
           #把url地址里面的东西读出来给soup
           try:#用try来解决有时候编码utf-8不好使的问题
            html = urlopen(url).read().decode('utf-8')
           except Exception:
                  continue
           soup = BeautifulSoup(html, features='lxml')
           #把地址里面的每一个页码地址都取出来
           #这个正则表达式表示开头必须是/p的,里面有/p后很多数字的
           sub_urls = soup.find_all("a", {"target": "_blank", "href": re.compile("(?=^/p)/p/d*")})
           
           for i in range(len(sub_urls)):
            a=sub_urls[i]['href']#利用这个方式提取一个网址里面的href
            
            
            #这里面每一个a就是要爬取的网页
            #下面开始爬取他的图片
            baseurl='https://tieba.baidu.com'
            url=baseurl+str(a)
            try:
             html = urlopen(url).read().decode('utf-8')
            except Exception:
                  continue
            soup = BeautifulSoup(html, features='lxml')
            
            #soup里面进入标签的方法是.标签就行了所谓标签就是<a>...<a>这里面东西就是a标签里面的内容
            
            tupian=soup.cc
    
    
    
            #先设置一下保存的位置:
            dir=r'D:	upian'
            
    #这个目录先自己手动创立一下
            #然后直接find img get src但是有的cc里面没有图片
            
            try:
              print (333)
              img_src2 = tupian.find('img').get('src')
              print (333999)
              pic_name=str(num)
              print (img_src2)
              #这个pic_name还必须要写一个拓展名
              pic_name=pic_name+img_src2[-4:]
              pic_name=os.path.join(dir,pic_name)
              urllib.request.urlretrieve(img_src2, pic_name)
              
              num+=1
            except Exception:
                  continue
            
            
              
    
           
    ##
    ##
    ##      
    ##his = ["/item/%E9%B8%A1%E7%9F%A2%E8%97%A4/931175?fromtitle=%E9%B8%A1%E5%B1%8E%E8%97%A4&fromid=374872"]
    ##
    ##for i in range(200):
    ## url = base_url + his[-1]
    ##
    ##print (77777777)
    ##
    ##html = urlopen(url).read().decode('utf-8')
    ##print (8888888888)
    ##soup = BeautifulSoup(html, features='lxml')
    ##print (9999999)
    ##
    ##print(i, soup.find('h1').get_text(), ' url: ',base_url+ his[-1])
    ##
    ### find valid urls
    ##sub_urls = soup.find_all("a", {"target": "_blank", "href": re.compile("(?=^/item)/item/(%.{2})+$")})
    ##
    ##
    ##print ('爬到了'+str(i+1))
    ##if len(sub_urls) != 0 :
    ##
    ## his.append(random.sample(sub_urls, 1)[0]['href'])
    ##else:
    ## if len(his)==1:
    ##  pass
    ## else:
    ### no valid sub link found
    ##  his.pop()
  • 相关阅读:
    sublime text在Linux下build出现'ascii' codec can't decode byte 0xe4 in position 0的解决办法
    sublime text的verilog插件——verilog automatic
    Ubuntu Lucid Installation Guide
    vim之perlsupport的小修改
    python笔记
    sublime text中ctags使用注意事项
    2021年终总结
    wp7学习站点
    WPF中调用Winform控件
    Xen Cloud Platform 系列:NFS 存储库(四)
  • 原文地址:https://www.cnblogs.com/zhangbo2008/p/8445143.html
Copyright © 2011-2022 走看看