领先的免费Web技术教程,涵盖HTML到ASP.NET

网站首页 > 知识剖析 正文

Python数据分析之数据采集:使用3个线程采集电子商务网站数据

nixiaole 2025-06-08 23:19:35 知识剖析 3 ℃


《大数据和人工智能交流》头条号向广大初学者新增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")


Tags:

最近发表
标签列表