python 运行完成无法终止程序
本帖最后由 ∫護着妳佉遠方 于 2022-1-31 15:45 编辑下载完成以后,程序还在运行,没有退出,不知道为什么
求助:问题所在,解决方法
import re
import time
import urllib.parse
import urllib.request
from multiprocessing import Queue, Process
import requests
from bs4 import BeautifulSoup
# 生产者
def producer(name_, q):
url = 'https://www.starbucks.com.cn/menu/'
head = {
'Cookie': 'ZHh6ku4z=AMCsJHZ-AQAAOcyxX6Yl2s20HjtkZfHK87MTPofIn6iYXJHzUvqd_HT1ZDMD|1|0'
'|36c0e1a8addcb91415dc5a8a4223425eca69e3d4',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, '
'like Gecko) Chrome/97.0.4692.99 Safari/537.36 Edg/97.0.1072.76'
}
# 请求对象定制
request = urllib.request.Request(url=url, headers=head)
#模拟浏览器向服务器发送请求
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
# print(content)
soup = BeautifulSoup(content, 'lxml')
# 名称
# //ul[@class="grid padded-3 product"]//strong/text()
# 名称列表
name_list = soup.select('ul strong')
# 图片列表.jpg
# //ul[@class="grid padded-3 product"]//div/@style
img_lisi = soup.select('ul div')
for i in range(len(name_list)):
name = name_list
aaa = name.get_text()
yyy = re.sub('/', ',', aaa)
img = img_lisi
bbb = img.attrs.get('style')
ccc = re.findall(r'url\("(.*)"\)', bbb)
ddd = 'https://www.starbucks.com.cn' + ccc
q.put({'name': yyy, 'img': ddd})
# print('生产完毕')
# 消费者
def consumer(name_, q):
while True:
res = q.get()
if q.qsize() == 0 or q.empty():
break
else:
name1 = res['name']
img1 = res['img']
# print(name_, name1, img1)
f = open(name1 + '.jpg', 'wb')
d = requests.get(img1).content
f.write(d)
f.close()
print(name_, '成功下载', name1)
if __name__ == '__main__':
# 创建一个队列
q = Queue()
p1 = Process(target=producer, args=('生产者', q))
p1.start()
for i in range(6):
c = Process(target=consumer, args=('消费者' + str(i), q))
c.start()
https://s4.ax1x.com/2022/01/31/HiPDB9.png 感谢分享,学习学习 感谢分享,正好饿了 看不懂 但觉得厉害好 大佬niup 学习了,高手。 哈哈, 这么猛的吗.
好好玩~
感谢分享! 代码运行不了.
libraries都安装了.
然后python main.py开始运行之后, 就卡住...不知道为啥. tewboo 发表于 2022-1-29 07:13
代码运行不了.
libraries都安装了.
然后python main.py开始运行之后, 就卡住...不知道为啥.
应该可以啊,只是没办法停止,需要手动才能停下来,这个问题我还没解决 tewboo 发表于 2022-1-29 07:13
代码运行不了.
libraries都安装了.
然后python main.py开始运行之后, 就卡住...不知道为啥.
如果一直不行,你试试这个串行的"""
* @codding : utf-8
* @time: 2022/1/8 19:10:50
* @name : 18_BS4爬取星巴克.py
* @system: Windows 11
* @Types : pythonProject
* @software : PyCharm
* @author :
"""
import os
import re
import time
from urllib import request
from bs4 import BeautifulSoup
import requests
url = 'https://www.starbucks.com.cn/menu/'
response = request.urlopen(url)
content = response.read().decode('utf-8')
soup = BeautifulSoup(content, 'lxml')
# 名称
# //ul[@class="grid padded-3 product"]//strong/text()
# 名称集合
name_list = soup.select('ul strong')
# 图片集合.jpg
# //ul[@class="grid padded-3 product"]//div/@style
img_lisi = soup.select('ul div')
# print(name_list)
# print(img_lisi)
# 名称
list1 = []
# 图片地址
list2 = []
for name in name_list:
aaa = name.get_text()
yyy = re.sub('/', ',', aaa)
list1.append(yyy)
for img in img_lisi:
bbb = img.attrs.get('style')
ccc = re.findall(r'url\("(.*)"\)', bbb)
ddd = 'https://www.starbucks.com.cn' + ccc
list2.append(ddd)
# list1 名称 list2 地址
# 下载
# 开始时间
os.chdir('/爬虫/爬虫基础\\18_BS4爬取星巴克')
start_time = time.time()
i = 0
for i in range(len(list1)):
# print(list1)
# print(list2)
# 请先创建文件夹
if i == 0:
print('开始下载!!!')
f = open(list1 + '.jpg', 'wb')
d = requests.get(list2).content
f.write(d)
f.close()
print('成功下载第{}张'.format(i + 1))
# if i % 10 == 0:
# time.sleep(1)
# 结束时间
end_time = time.time()
print('下载完成!!!')
otherStyleTime = time.strftime("%S", time.localtime(end_time - start_time))
print('本次执行共下载{}张图,共耗时{}秒'.format(i, otherStyleTime))
页:
[1]
2