python通过urllib2爬网页上种子下载示例

 更新时间:2014年02月24日 14:43:33   作者:  
这篇文章主要介绍了通过urllib2、re模块抓种子下载的示例,需要的朋友可以参考下

通过urllib2、re模块抓种子

思路

1.用程序登录论坛(如果需要登录才能访问的版块)

2.访问指定版块

3.遍历帖子(先取指定页,再遍历页面所有帖子的url)

4.循环访问所有帖子url,从帖子页面代码中取种子下载地址(通过正则表达式或第三方页面解析库)

5.访问种子页面下载种子

复制代码 代码如下:

import urllib
import urllib2
import cookielib
import re
import sys
import os

# site is website address | fid is part id
site = "http://xxx.yyy.zzz/"
source = "thread0806.php?fid=x&search=&page="

btSave = "./clyzwm/"
if os.path.isdir(btSave):
 print btSave + " existing"
else:
 os.mkdir(btSave)

logfile = "./clyzwm/down.log"
errorfile = "./clyzwm/error.log"
sucfile = "./clyzwm/sucess.log"

headers = {'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36', 
           'Referer' : 'http://xxx.yyy.zzz/'}

def btDown(url, dirPath):
 logger(logfile, "download file : " + url)
 try:
  #pageCode = urllib2.urlopen(url).read()
  #print pageCode
  btStep1 = re.findall('http://[\w]+\.[\w]+\.[\w]{0,4}/[\w]{2,6}\.php\?[\w]{2,6}=([\w]+)', url, re.I)
  #print btStep1
  if len(btStep1)>0:
   ref = btStep1[0]
   downsite = ""
   downData = {}
   if len(ref)>20:
    downsite = re.findall('http://www.[\w]+\.[\w]+/', url)[0]
    downsite = downsite + "download.php"
    reff = re.findall('input\stype=\"hidden\"\sname=\"reff\"\svalue=\"([\w=]+)\"', urllib2.urlopen(url).read(), re.I)[0]
    downData = {'ref': ref, 'reff':reff, 'submit':'download'}
   else:
    downsite = "http://www.downhh.com/download.php"
    downData = {'ref': ref, 'rulesubmit':'download'}
   #print "bt site - " +  downsite + "\n downData:"
   #print downData
   downData = urllib.urlencode(downData)
   downReq = urllib2.Request(downsite, downData)
   downReq.add_header('User-Agent','Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36')
   downPost = urllib2.urlopen(downReq)
   stream = downPost.read(-1)
   if (len(stream) > 1000):
    downPost.close()
    name = btStep1[0]+ ".torrent"
    fw = open(dirPath + name, 'w')
    fw.write(stream)
    fw.close()
    logger(sucfile, url+"\n")
   else:
    logger(errorfile, url+"\n")
 except urllib2.URLError, e:
  print e.reason

def logger(logfile, msg):
 print msg
 fw = open(logfile, 'a')
 fw.write(msg)
 fw.close()

for i in range(1, 1000):
 logger(logfile, "\n\n\n@ page " + str(i) + " ...")
 part = site + source + str(i)

 content = urllib2.urlopen(part).read()
 content = content.decode('gbk').encode('utf8')
 #print content

 pages = re.findall('<a\s+href=\"(htm_data/[\d]+/[\d]+/[\d]+\.html).*?<\/a>', content,re.I)
 #print pages

 for page in pages:
  page = site + page;
  #logger(logfile, "\n# visiting " + page + " ...")
  pageCode = urllib2.urlopen(page).read()
  #print pageCode
  zzJump = re.findall('http://www.viidii.info/\?http://[\w]+/[\w]+\?[\w]{2,6}=[\w]+' ,pageCode)  
  #zzJump = re.findall('http://www.viidii.info/\?http://[\w/\?=]*', pageCode)
  if len(zzJump) > 0:
   zzJump = zzJump[0]
   #print "- jump page - " + zzJump
   pageCode = urllib2.urlopen(page).read()
   zzPage = re.findall('http://[\w]+\.[\w]+\.[\w]+/link[\w]?\.php\?[\w]{2,6}=[\w]+' ,pageCode)
   if len(zzPage) > 0:
    zzPage = zzPage[0]
    logger(logfile, "\n- zhongzi page -" + zzPage)
    btDown(zzPage, btSave)
   else:
    logger(logfile, "\n. NOT FOUND .")
  else:
   logger(logfile, "\n... NOT FOUND ...")
  zzPage = re.findall('http://[\w]+\.[\w]+\.[\w]+/link[\w]?\.php\?ref=[\w]+' ,pageCode)

相关文章

  • Python字典创建 遍历 添加等实用基础操作技巧

    Python字典创建 遍历 添加等实用基础操作技巧

    字段是Python是字典中唯一的键-值类型,本文讲述了Python中字典如何创建 遍历 添加等实用基础操作技巧,内容非常基础但非常重要,一定要熟练掌握
    2018-09-09
  • wtfPython—Python中一组有趣微妙的代码【收藏】

    wtfPython—Python中一组有趣微妙的代码【收藏】

    Wtfpython讲解了大量的Python编译器的内容。这篇文章主要介绍了wtfPython-Python中一些奇妙的代码,感兴趣的朋友跟随脚本之家小编一起看看吧
    2018-08-08
  • Python OpenGL绘制一场烟花盛会

    Python OpenGL绘制一场烟花盛会

    正值新春佳节,小编今天为大家带来了用Python OpenGL绘制的一场烟花盛会,文中的实现步骤讲解详细,感兴趣的小伙伴可以跟随小编一起动手试一试
    2022-02-02
  • Python基础之numpy库的使用

    Python基础之numpy库的使用

    这篇文章主要介绍了Python基础之numpy库的使用,文中有非常详细的代码示例,对正在学习python基础的小伙伴们有非常好的帮助,需要的朋友可以参考下
    2021-04-04
  • python抓取网页内容并进行语音播报的方法

    python抓取网页内容并进行语音播报的方法

    今天小编就为大家分享一篇python抓取网页内容并进行语音播报的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-12-12
  • python使用UDP实现客户端和服务器对话

    python使用UDP实现客户端和服务器对话

    这篇文章主要为大家介绍了python使用UDP实现客户端和服务器对话示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • python中resample函数实现重采样和降采样代码

    python中resample函数实现重采样和降采样代码

    今天小编就为大家分享一篇python中resample函数实现重采样和降采样代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02
  • Python实现各种邮件发送

    Python实现各种邮件发送

    这篇文章主要介绍了Python实现各种邮件发送,Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件,下文详细实现过程需要的小伙伴可以参考一下
    2022-05-05
  • 使用Python创建自助抢单插件的完整步骤

    使用Python创建自助抢单插件的完整步骤

    文章介绍了如何使用Python编写一个自助抢单插件,该插件可以帮助用户监控特定网站上的商品信息,并在条件满足时自动下单,文章涵盖了从项目概述、技术架构、项目流程到环境准备、网络请求、数据解析、用户界面设计和定时任务的详细步骤
    2024-11-11
  • 使用Python读取json文件的方法小结

    使用Python读取json文件的方法小结

    这篇文章主要给大家介绍了Python读取json文件的方法,使用python读取json文件,输出结果为字符串或python对象,文中有详细的代码示例和图解,感兴趣的小伙伴可以自己动手试一试
    2023-09-09

最新评论