网站首页 > 知识剖析 正文
《大数据和人工智能交流》头条号向广大初学者新增C 、Java 、Python 、Scala、javascript 等目前流行的计算机、大数据编程语言,希望大家以后关注本头条号更多的内容。
使用python编写3个线程同时采集商品“新品上架”的信息:
一、页面参考信息
页面部分代码如下:
<div id="dome">
<div id="dome1">
<dl>
<dt><img src="images/guimei_scroll_01.jpg" alt="scroll" /></dt>
<dd>大牌狂降价,三折直送</dd>
</dl>
<dl>
<dt><img src="images/guimei_scroll_02.jpg" alt="scroll" /></dt>
<dd>大学老师开网店</dd>
</dl>
<dl>
<dt><img src="images/guimei_scroll_03.jpg" alt="scroll" /></dt>
<dd>黑眼圈推荐,美白不停</dd>
</dl>
<dl>
<dt><img src="images/guimei_scroll_04.jpg" alt="scroll" /></dt>
<dd>瘦身狂潮风,修形之选</dd>
</dl>
<dl>
<dt><img src="images/guimei_scroll_01.jpg" alt="scroll" /></dt>
<dd>大牌狂降价,三折直送</dd>
</dl>
<dl>
<dt><img src="images/guimei_scroll_02.jpg" alt="scroll" /></dt>
<dd>大学老师开网店</dd>
</dl>
<dl>
<dt><img src="images/guimei_scroll_03.jpg" alt="scroll" /></dt>
<dd>黑眼圈推荐,美白不停</dd>
</dl>
<dl>
<dt><img src="images/guimei_scroll_04.jpg" alt="scroll" /></dt>
<dd>瘦身狂潮风,修形之选</dd>
</dl>
<dl>
<dt><img src="images/guimei_scroll_01.jpg" alt="scroll" /></dt>
<dd>大牌狂降价,三折直送</dd>
</dl>
<dl>
<dt><img src="images/guimei_scroll_02.jpg" alt="scroll" /></dt>
<dd>大学要求老师开网店</dd>
</dl>
<dl>
<dt><img src="images/guimei_scroll_03.jpg" alt="scroll" /></dt>
<dd>黑眼圈推荐,美白不停</dd>
</dl>
<dl>
<dt><img src="images/guimei_scroll_04.jpg" alt="scroll" /></dt>
<dd>瘦身狂潮风,修形之选</dd>
</dl>
<dl>
<dt><img src="images/guimei_scroll_01.jpg" alt="scroll" /></dt>
<dd>大牌狂降价,三折直送</dd>
</dl>
<dl>
<dt><img src="images/guimei_scroll_02.jpg" alt="scroll" /></dt>
<dd>大学老师开网店</dd>
</dl>
<dl>
<dt><img src="images/guimei_scroll_03.jpg" alt="scroll" /></dt>
<dd>黑眼圈推荐,美白不停</dd>
</dl>
<dl>
<dt><img src="images/guimei_scroll_04.jpg" alt="scroll" /></dt>
<dd>瘦身狂潮风,修形之选</dd>
</dl>
<dl>
<dt><img src="images/guimei_scroll_03.jpg" alt="scroll" /></dt>
<dd>黑眼圈推荐,美白不停</dd>
</dl>
<dl>
<dt><img src="images/guimei_scroll_04.jpg" alt="scroll" /></dt>
<dd>瘦身狂潮风,修形之选</dd>
</dl>
<dl>
<dt><img src="images/guimei_scroll_01.jpg" alt="scroll" /></dt>
<dd>大牌狂降价,三折直送</dd>
</dl>
</div>
<div id="dome2"></div>
</div>
二、参考代码
from bs4 import BeautifulSoup
import threading # 导入线程库
import time
# 打开文件
with open('index.html', encoding="GB2312") as data:
content = data.read()
soup = BeautifulSoup(content, 'html.parser')
# 得到数据
datas = soup.find_all('div', attrs={'id': 'dome1'})
labels = datas[0].find_all('dl')
# 爬取数据
def print_label(threadName, labels):
for label in labels:
time.sleep(i // 10)
info = label.find_all('dd')
print("线程%d获取到数据:=====%s\n"%(threadName,info[0].get_text()))
threads = []
# 创建线程对象
for i in range(3):
split = int(len(labels) / 3)
if i == 2:
# 创建新线程并添加到线程列表
threads.append(threading.Thread(target=print_label, args=(i, labels[split * i:])))
else:
threads.append(threading.Thread(target=print_label, args=(i, labels[split * i:split * (i + 1)])))
# 运行线程
for th in threads:
th.start()
# 等待线程退出
for th in threads:
th.join()
print("finished")
- 上一篇: 刷屏的 ChatGPT,它究竟能干什么?
- 下一篇: 好端端的线程池,怎么就卡死了?(线程池处理流程)
猜你喜欢
- 2025-06-08 面试题:谈谈进程切换与线程切换的区别?
- 2025-06-08 构建无锁的线程安全架构:掌握Java中ThreadLocal的原理灵活应用
- 2025-06-08 好端端的线程池,怎么就卡死了?(线程池处理流程)
- 2025-06-08 刷屏的 ChatGPT,它究竟能干什么?
- 2025-06-08 Python爬虫进阶教程(二):线程、协程
- 2025-06-08 深入解析线程池,就这一篇(线程池理解)
- 2025-06-08 嘘,我悄悄地告诉你为什么ThreadLocal容易导致内存泄漏?
- 2025-06-08 储钱罐:注册就送1000元!钱我出,收益你拿走!
- 2025-06-08 不折腾会死---浅谈家庭局域网的搭建
- 2025-06-08 3 个高级 JavaScript 面试问题(js面试题必问)
- 最近发表
- 标签列表
-
- xml (46)
- css animation (57)
- array_slice (60)
- htmlspecialchars (54)
- position: absolute (54)
- datediff函数 (47)
- array_pop (49)
- jsmap (52)
- toggleclass (43)
- console.time (63)
- .sql (41)
- ahref (40)
- js json.parse (59)
- html复选框 (60)
- css 透明 (44)
- css 颜色 (47)
- php replace (41)
- css nth-child (48)
- min-height (40)
- xml schema (44)
- css 最后一个元素 (46)
- location.origin (44)
- table border (49)
- html tr (40)
- video controls (49)