吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3141|回复: 5
收起左侧

[Python 转载] 多线程爬取某贴吧帖子

[复制链接]
Lobolang 发表于 2019-8-14 21:03
本帖最后由 Lobolang 于 2019-8-14 22:24 编辑

由于刚学没多久,技术有限,有不少错误望各位师兄师姐多多指点..
[Python] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import requests
import threading
from lxml import etree
from queue import Queue
 
headers={'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Mobile Safari/537.36'}
 
class TiebaSpider:
    def __init__(self):
        self.url='http://tieba.baidu.com/f?&kw=lol&pn={}'     #本代码以lol为例子,lol可更换为其他贴吧的吧名
        self.url_queen = Queue()
        self.response_queen = Queue()
 
    def get_urllist(self):
        for i in range(0,5):
            self.url_queen.put(self.url.format(i*30))
 
    def get_response(self):
        while True:
            url = self.url_queen.get()
            response=requests.get(url,headers=headers)
            self.response_queen.put(response.content.decode())
            self.url_queen.task_done()
 
    def get_info(self):
        while True:
            html = etree.HTML(self.response_queen.get())
            title=html.xpath('//div[@class="ti_title"]/span[not(contains(text(),"置顶"))][not(contains(@class,"icon"))]/text()')
            title_url=html.xpath('//ul[@class="threads_list"]//a[not(contains(@class,"ti_item j_click_stats"))][not(contains(text(),"立即查看"))]/@href')
            str='http://tieba.baidu.com'
            url=[str+i for i in title_url]
            count=0
            while count<len(title):
                print('标题:%s 地址:%s' %(title[count],url[count]))
                count+=1
            self.response_queen.task_done()
 
    def run(self):
        thread_list=[]
        for i in range(1):                   #获取页码的线程数:1     线程数可根据自己喜好更改
            t_list=threading.Thread(target=self.get_urllist)
            thread_list.append(t_list)
        for i in range(10):                #获取响应的线程数:10
            t_response=threading.Thread(target=self.get_response)
            thread_list.append(t_response)
        for i in range(3):                  #采集数据的线程数:3
            t_info=threading.Thread(target=self.get_info)
            thread_list.append(t_info)
 
        for t in thread_list:
            t.setDaemon(True)
            t.start()
 
        for p in (self.url_queen,self.response_queen):
            p.join()                                         #设置主线程等待其他线程结束再结束
 
        print("\33[36m获取结束")
 
if __name__ == '__main__':
    q=TiebaSpider()
    q.run()

免费评分

参与人数 2热心值 +2 收起 理由
天气敷和 + 1 用心讨论,共获提升!
逆向学习 + 1 热心回复!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| Lobolang 发表于 2019-8-14 21:06
xpath使用不太了解,有时候遇到只有图片没有title内容的帖子就获取不到标题,然后后面标题和网址就乱了,或者就列表溢出,,,,经排查发现是获取title的xpath中的span标签为空,,,我也不知道这个怎么写,望有师兄师姐指点指点
JavaSB 发表于 2019-8-14 21:17
逆向学习 发表于 2019-8-14 21:18
夏橙M兮 发表于 2019-8-14 21:36
谢谢分享。
天气敷和 发表于 2019-8-14 22:39
感谢热心分享!
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-7-12 06:24

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表