python爬虫爬取女u番号和磁力链接,封面,保存到csv文件
2017-08-09 14:27
1286 查看
暑假很无聊,学了一个月的爬虫,前两天写了一个爬取男人团上面的番号然后用番号到种子在线搜索的网站爬取磁力链接最后保存到csv文件的一个小案例,自己感觉很有趣,写一下大概的过程吧,给比我还新的爬虫选手参考参考。
首先进入男人团的网站,选择一个女u,这里我选的波多老师,我们看第一页网页http://nanrenvip.net/boduoyejieyi/发现后面就是波多野结衣的拼音。再看下第二页http://nanrenvip.net/boduoyejieyi/index_2.html就后面加了index_2.html以此类推后面多少页就改一个数字就行了。第一页不能用index_1亲测404。第一步先获得总共的页数,打开第一页拉到最下面,
在尾页的地方右键查看元素
所以一共18页。
获取页数的代码:
每一个作品右键选择查看源代码,观察源代码发现每一个li标签代表一个作品,里面包含了一些我们需要的信息, 番号,封面,标题,时间,使用Beautifulsoup分别找到他们
代码:
现在我们已经把所有需要的信息都添加在了info的列表里,现在该写入文件了。 多线程下载封面
获取种子,网上随便找的在线种子搜索,abc-123代表番号/1-3是Hot的意思,就选最火热的一个。然后右键获取源代码,得到地址。
打开地址,按照之前使用Beautiful找到标签,获取内容的方法就能获取到磁力地址了
代码:
写入csv文件:
结果:
代码全部在: https://code.csdn.net/qq_22313461/learn_spider/tree/master/learn.py 改下代码中的名字 就能获取不同人的信息 写的乱七八糟。 0.0
获取页数的代码:
def get_pages(): print('start get pages_num.') lis = [] try: url = 'http://nanrenvip.net/' + keyword + '/' #构建第一页的url html = urllib.request.urlopen(url,timeout=5) #打开第一页,超时设置为5s bsObj = BeautifulSoup(html,'lxml') lis = bsObj.find('div',{'class':'dede_pages'}).find_all('li')#找到所有的页码 except Exception as e: print(e) if len(lis)>1: #如果页数大于1 获取总页数 pages = lis[-1].a['href'].split('_')[-1].replace('.html','') #最后一个[-1] else: pages = 1 #否则页数为1 print('pages = ',pages) return int(pages)
每一个作品右键选择查看源代码,观察源代码发现每一个li标签代表一个作品,里面包含了一些我们需要的信息, 番号,封面,标题,时间,使用Beautifulsoup分别找到他们
代码:
def get_one_page(pages): print('begin get_one page') global page alls = [] #所有作品的列表 page = page + 1 if page <= pages: #如果page数小于总数 if page == 1: url ='http://nanrenvip.net/'+keyword+'/' #第一页的url else: url = 'http://nanrenvip.net/'+keyword+'/index_'+str(page)+'.html' #后面的url print(url) try: html = urllib.request.urlopen(url) bsObj = BeautifulSoup(html,'lxml') alls = bsObj.find_all('li',{'class':'post'}) #先找到所有class属性为post的li标签就是每一个作品 except Exception as e: print(e) for each in alls: #对每个作品 try: fengmian = '没获取到' #初始化地址,封面地址里有-small和small-小图的意思,把它去掉获得高清大图 #找到img标签,对照网页源代码data-original属性就是地址 fengmian = each.find('img')['data-original'].replace('small-','').replace('-small','') except Exception as e: print(e) try: fanhao = '没获取到' fanhao = each.find('b').get_text()#番号在li标签的b标签里 except Exception as e: print(e) try: the_time = '没获取到' shijian = each.find_all('date') the_time = shijian[1].get_text() #获取第二个date标签里的text就是日期 except Exception as e: print(e) try: title = '没获取到' title = each.find('p').get_text() #li下面的p标签装的是名字 except Exception as e: print(e) info.append([fanhao,the_time,title,fengmian]) #这个info是一个全局对象 创建在最前面 info = [] time.sleep(3) #停3s太快可能会被封 get_one_page(pages) #继续获取信息
现在我们已经把所有需要的信息都添加在了info的列表里,现在该写入文件了。 多线程下载封面
def get_fengmian(): global info i = 0 dir = 'good/' + keyword + '/' if not os.path.exists(dir): #判断路径是否存在 不存在就创建 os.makedirs(dir) def process_queue(i): #等会创建线程的函数 try: urllib.request.urlretrieve(info[i][3],filename=dir + info[i][0]+'.jpg') #下载 except Exception as e: print(e) threads = [] while i < len(info): #i小于info的长度 for thread in threads: #判断每一个thread是否存活,没存活就去掉 if not thread.is_alive(): threads.remove(thread) while len(threads) < 5 and i < len(info): thread = threading.Thread(target=process_queue,args=(i,)) #创建线程调用process_queue参数为i thread.setDaemon(True) thread.start() #开始 threads.append(thread) #添加到threads的列表 i += 1 #下一个info time.sleep(3)
获取种子,网上随便找的在线种子搜索,abc-123代表番号/1-3是Hot的意思,就选最火热的一个。然后右键获取源代码,得到地址。
打开地址,按照之前使用Beautiful找到标签,获取内容的方法就能获取到磁力地址了
代码:
def get_zhongzi(): print('start get zhongzi') i = 0 def process_queue(i): #因为我们之前apend的时候是append的列表所以对于每一个的番号就是info[i][0] search_url = 'http://www.btwhat.net/search/'+info[i][0]+'/1-3.html' #构建地址 try: html = urllib.request.urlopen(search_url,timeout=5).read() bsObj = BeautifulSoup(html,'lxml') #获取第一个的地址 choose_hot_one = 'http://www.btwhat.net'+bsObj.find('div',{'class':'search-item'}).a['href'] zhongzi_page = urllib.request.urlopen(choose_hot_one,timeout=5) bsObj = BeautifulSoup(zhongzi_page,'lxml') cili = '没获取到' if bsObj.find('div',{'class':'panel-body'}): cili = bsObj.find('div',{'class':'panel-body'}).a['href'] #获取种子 info[i].append(cili) #对每一个append磁力链接 print(info[i]) except Exception as e: print(e) i = 0 #和前面一样 多线程 threads = [] while i < len(info): for thread in threads: if not thread.is_alive(): threads.remove(thread) while i<len(info) and len(threads)<5: thread = threading.Thread(target=process_queue,args=(i,)) thread.setDaemon(True) thread.start() threads.append(thread) i += 1 time.sleep(SLEPP_TIME)
写入csv文件:
def write_to_csv(): print('xieru csv') filename = 'good/'+keyword+'/'+keyword+'.csv' path = os.path.dirname(filename) if not os.path.exists(path): os.makedirs(path) csvfile = open(filename,'w') writer = csv.writer(csvfile) try: writer.writerow(['番号','上映时间','标题','封面地址','种子']) except Exception as e: print(e) global info def process_queue(i): try: writer.writerow(info[i]) except Exception as e: print(e) i = 0 threads = [] while i < len(info): for thread in threads: if not thread.is_alive(): threads.remove(thread) while i < len(info) and len(threads) < 10: thread = threading.Thread(target=process_queue,args=(i,)) thread.setDaemon(True) thread.start() threads.append(thread) i += 1 csvfile.close()
结果:
代码全部在: https://code.csdn.net/qq_22313461/learn_spider/tree/master/learn.py 改下代码中的名字 就能获取不同人的信息 写的乱七八糟。 0.0
相关文章推荐
- python selenium登录企名片,筛选各种条件,爬虫爬取融资信息,保存到csv文件
- 使用python的pandas库读取csv文件保存至mysql数据库
- python的pandas工具包,保存.csv文件时不要表头的实例
- python爬虫 学习 中国大学排名显示及保存文件 DAY3
- Python数据处理-将数据保存为txt、csv等文件格式方法
- Python爬虫学习--WIN10下定时获取CSDN个人的访问量并保存到文件中2018/03/05
- php从爬虫爬取的txt文件按行读取并写入保存到excel,csv中
- Python爬虫小实践:寻找失踪人口,爬取失踪儿童信息并写成csv文件,方便存入数据库
- python通过串口读取GPS NMEA格式的数据,并保存为csv文件
- 用python写一个简单的爬虫保存在json文件中
- 单个爬虫文件使用scrapy保存为csv格式
- scrapy爬虫保存为csv文件的技术分析
- Python爬虫案例2:获取抽屉网页所有的新闻标题并保存到csv
- python 保存csv文件
- BeautifulSoup爬虫之保存CSV文件
- python selenium爬虫实践:获取自如租房数据保存到文件
- 不务正业--用python爬虫抓取Konachan的图片并保存到本地文件
- Python爬虫学习--WIN10下定时获取CSDN个人的访问量并保存到文件中2018/01/19
- 利用python抓取搜狗关于数据分析的文章并保存到csv文件
- python scrapy爬取腾讯招聘网站保存为csv文件,而且设定其表头顺序(csv默认是无序的)