V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
POP
V2EX  ›  Python

求帮我修改一下爬虫,能把它改成多线程的吗?初学 py!求帮帮忙,找的别人的源码!

  •  
  •   POP · 2015-01-15 13:08:44 +08:00 · 4264 次点击
    这是一个创建于 3388 天前的主题,其中的信息可能已经有所发展或是发生改变。
    # -*- coding: utf-8 -*-
    import urllib2
    import sys
    #BeautifulSoup3不需要修改,BeautifulSoup4,改成from bs4 import BeautifulSoup
    from BeautifulSoup import BeautifulSoup

    reload(sys)
    sys.setdefaultencoding( "utf-8" )

    def getcontent(url):
    print url
    req = urllib2.Request(url)
    res = urllib2.urlopen(req)
    magnetlist=[]
    html = res.read()
    res.close()
    soup = BeautifulSoup(html)
    #BeautifulSoup3不需要修改,BeautifulSoup4,改成soup.find_all('a')
    allentry=soup.findAll('a')
    for link in allentry:
    if "magnet:"==link.get('href')[0:7]:
    magnetlist.append(link.get('href'))
    magnetlist = [line+'\n' for line in magnetlist]
    f =open("magnet.txt", "a")
    f.writelines(magnetlist)
    f.close()

    def main():
    site="http://bt.shousibaocai.com/search/"
    keyword="地心引力"
    keyword=urllib2.quote(keyword)
    #总共抓前多少页
    page=3
    for i in range(1,page):
    searchurl=site+keyword+"/"+str(i)
    getcontent(searchurl)

    if __name__ == '__main__':
    main()
    #end Jarett
    27 条回复    2015-01-16 15:31:44 +08:00
    tuuuz
        1
    tuuuz  
       2015-01-15 13:43:23 +08:00   ❤️ 1
    看的好累
    POP
        2
    POP  
    OP
       2015-01-15 13:47:32 +08:00
    (T_T)
    mhycy
        3
    mhycy  
       2015-01-15 14:07:34 +08:00   ❤️ 1
    初学还别人的源码,为何不自己改呢?就是一个学习的过程而已~
    Kilerd
        4
    Kilerd  
       2015-01-15 14:21:54 +08:00   ❤️ 1
    python的缩进,一贴上来就全乱了,怎么看,扯淡啊
    LZ你压根就不懂py吧,,拒绝伸手党
    POP
        5
    POP  
    OP
       2015-01-15 14:37:34 +08:00
    @Kilerd 算了,我还是自己去改吧。。。贴代码的缩进问题,我还真没注意到
    POP
        6
    POP  
    OP
       2015-01-15 14:40:40 +08:00
    @mhycy 受教了
    justjavac
        7
    justjavac  
       2015-01-15 14:42:54 +08:00
    已block
    bigtan
        8
    bigtan  
       2015-01-15 14:47:34 +08:00   ❤️ 1
    http://segmentfault.com/blog/caspar/1190000000414339

    参考这个

    我自己使用过一次,效果很好
    POP
        9
    POP  
    OP
       2015-01-15 14:53:19 +08:00
    @bigtan 谢谢,我在尝试用线程池改
    virusdefender
        10
    virusdefender  
       2015-01-15 15:51:16 +08:00   ❤️ 1
    http://www.virusdefender.net/index.php/archives/84/

    把我的代码最下面的 job 函数和循环添加任务的函数换成你的就行了~
    Septembers
        11
    Septembers  
       2015-01-15 16:02:30 +08:00
    POP
        12
    POP  
    OP
       2015-01-15 17:00:18 +08:00
    @virusdefender 我还是先自己改改看。。。谢谢了
    lxkaka
        13
    lxkaka  
       2015-01-15 17:17:57 +08:00   ❤️ 1
    python 的多线程无法榨干cpu吧,要用多进程
    POP
        14
    POP  
    OP
       2015-01-15 17:40:40 +08:00
    @lxkaka 我还是先修改源码吧。。。:(
    libo26
        15
    libo26  
       2015-01-15 18:47:48 +08:00
    个人用requests代替urllib2
    langxuan
        16
    langxuan  
       2015-01-15 19:15:15 +08:00
    不会是熊厂在过good coder吧。。。囧
    Anybfans
        17
    Anybfans  
       2015-01-15 19:41:31 +08:00   ❤️ 1
    为什么没缩进。。。我也初学,PY很注重缩进的。。我一般都是一个tab..不知道对不对,看别人都是4个空格。。
    O21
        18
    O21  
       2015-01-15 19:43:25 +08:00
    @Anybfans Tab党路过。。
    POP
        19
    POP  
    OP
       2015-01-15 20:23:28 +08:00
    @Anybfans 第一次贴代码。没注意。。。
    surewen
        20
    surewen  
       2015-01-15 23:10:53 +08:00
    要是v2ex支持markdown就好了
    Delbert
        21
    Delbert  
       2015-01-16 00:15:38 +08:00
    @surewen 已经支持。右边的入口是md的,正常的入口是没有md的。要贴代码用gist。
    pandada8
        22
    pandada8  
       2015-01-16 00:23:45 +08:00
    @lxkaka 爬虫基本是IO型,用多线程基本就够啦
    wezzard
        23
    wezzard  
       2015-01-16 01:29:29 +08:00 via iPhone
    你們都中招了,樓主是來黑Python的!
    icedx
        24
    icedx  
       2015-01-16 02:08:57 +08:00 via Android
    如果楼主不想深入学多* 程的哲学 可以试试map()
    ivanlw
        25
    ivanlw  
       2015-01-16 02:36:52 +08:00
    原来不只有我一个人看没有缩进的代码很累呀……怪不得天然适合python……
    xylophone21
        26
    xylophone21  
       2015-01-16 15:27:00 +08:00
    把range(1,page):这里的1,page两个参数改为从argc获取
    然后shell多启几个
    不会shell的话手动启也行,反正你也不会想要太多进程的.
    POP
        27
    POP  
    OP
       2015-01-16 15:31:44 +08:00
    @xylophone21 这都行!厉害!!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1470 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 17:27 · PVG 01:27 · LAX 10:27 · JFK 13:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.