logo
Loading...

如何使用多線程爬蟲做request.get回傳數值 - Cupoy

請問用多線程爬蟲後, 後續要如何用BeautifulSoup進行解析, 也就是該如何接受reques...

pycrawler

如何使用多線程爬蟲做request.get回傳數值

2020/01/14 上午 09:17
Python網路爬蟲討論版
Jeff Huang
觀看數:7
回答數:1
收藏數:2
pycrawler

請問用多線程爬蟲後, 後續要如何用BeautifulSoup進行解析, 也就是該如何接受request.get的回傳值, 謝謝

_thread.start_new_thread(requests.get, (url, ))

回答列表

  • 2020/01/15 下午 05:18
    張維元 (WeiYuan)
    贊同數:2
    不贊同數:0
    留言數:0

    嗨,如果是使用我們介紹的 _thread 內建套件的話無法取得回傳值(程式已經被分成兩的部分各自執行)。比較好的作法就是 Function 內處理後寫入資料庫或是外部檔案。


    另外有一個叫 threading 的第三方套件,可以做到取回回傳值,其作法如下:


    ```

    from threading import Thread

    def foo(bar):
       
    print 'hello {}'.format(bar)
       
    return 'foo'

    thread
    = Thread(target=foo, args=('world!',))
    thread
    .start()
    ret
    = thread.join()
    print(ret)

    ```


    這兩個套件的差異可以看這篇:https://stackoverflow.com/questions/5568555/thread-vs-threading