鱼C论坛

 找回密码
 立即注册
查看: 1425|回复: 5

pycharm调试没问题运行就卡住

[复制链接]
发表于 2020-5-20 13:47:21 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 xmpython 于 2020-5-20 19:43 编辑

今天用pycharm写了一个下载图片的多线程小爬虫,单步调试可以正常下载图片,但是一运行就会卡住,但是调试很正常啊,也不知道是咋回事,这是什么操作啊

发一下代码,由于爬的是一个不太正经的网站(没有反爬,你懂得
这是卡住截图
https://imgchr.com/i/YT9VyV

                               
登录/注册后可看大图


  1. import requests
  2. from fake_useragent import UserAgent
  3. import os
  4. import threading
  5. from lxml import etree
  6. from queue import Queue
  7. import time
  8. import re


  9. class Producer(threading.Thread):
  10.     headers = {
  11.         "Referer": "https://www.隐藏.com",
  12.         "User-Agent": UserAgent().random
  13.     }

  14.     def __init__(self, page_queue, img_queue):
  15.         threading.Thread.__init__(self)
  16.         self.page_queue = page_queue
  17.         self.img_queue = img_queue

  18.     def get_url(self, page_url):
  19.         re1 = requests.get(page_url, headers=self.headers)
  20.         e1 = etree.HTML(re1.text)
  21.         title_urls_half = e1.xpath("//div[@class='box list channel']/ul/li/a/@href")
  22.         zhu_url = get_zhuye()
  23.         for title_urls in title_urls_half:
  24.             title_url = zhu_url + title_urls
  25.             re2 = requests.get(title_url, headers=self.headers)
  26.             e2 = etree.HTML(re2.text)
  27.             img_urls = e2.xpath("//div[@class='content']/p/img/@src")
  28.             for img_url in img_urls:
  29.                 self.img_queue.put(img_url)
  30.                 # img_url 就是图片地址

  31.     def run(self):
  32.         while self.page_queue.empty() == False:
  33.             page_url = self.page_queue.get()
  34.             self.get_url(page_url)


  35. class Consumer(threading.Thread):
  36.     headers = {
  37.         "Referer": "https://www.隐藏.com",
  38.         "User-Agent": UserAgent().random
  39.     }

  40.     def __init__(self, page_queue, img_queue):
  41.         threading.Thread.__init__(self)
  42.         self.page_queue = page_queue
  43.         self.img_queue = img_queue

  44.     def run(self):
  45.         while True:
  46.             time.sleep(2)
  47.             if self.img_queue.empty():
  48.                 break
  49.             img_url = self.img_queue.get()
  50.             filename = img_url.split("/")[-1]
  51.             try:
  52.                 response = requests.get(img_url, headers=self.headers, timeout=5)
  53.                 print(img_url)
  54.                 with open(filename, "wb") as f:
  55.                     f.write(response.content)
  56.             except:
  57.                 print("超时")


  58. def get_zhuye():
  59.     headers = {
  60.         "Referer": "https://www.隐藏.com",
  61.         "User-Agent": UserAgent().random
  62.     }
  63.     base_url = "https://www.隐藏.com"
  64.     response = requests.get(base_url, headers=headers)
  65.     zhu_url = re.findall(r'window\.location\.href="(.+?)"', response.text)[0][:-1]
  66.     return zhu_url


  67. def main():
  68.     zhu_url = get_zhuye()
  69.     page_queue = Queue(50)
  70.     img_queue = Queue(1000)

  71.     a = int(input("输入下载开始的页码(≥2):"))
  72.     b = int(input("输入下载结束的页码(≥2):"))
  73.     for x in range(a, b + 1):
  74.         page_url = zhu_url + "/pic/2/index_%d.html" % x
  75.         page_queue.put(page_url)

  76.     for x in range(5):
  77.         t = Producer(page_queue, img_queue)
  78.         t.start()
  79.         # t.join()

  80.     for x in range(5):
  81.         t = Consumer(page_queue, img_queue)
  82.         t.start()
  83.         # t.join()


  84. if __name__ == '__main__':
  85.     images_path = os.path.join(os.path.dirname(__file__), "images")
  86.     if not os.path.exists(images_path):
  87.         os.mkdir(images_path)
  88.     os.chdir(images_path)
  89.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-5-20 13:48:47 | 显示全部楼层
本帖最后由 Twilight6 于 2020-5-20 13:50 编辑

怎么个卡法?截图吧
如何正确地发代码、上传图片和附件?
https://fishc.com.cn/thread-52272-1-1.html
(出处: 鱼C论坛)
图片上传QQ空间或者图床 复制图片链接 发上来
图片上传.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-20 14:13:31 | 显示全部楼层
Twilight6 发表于 2020-5-20 13:48
怎么个卡法?截图吧
如何正确地发代码、上传图片和附件?
https://fishc.com.cn/thread-52272-1-1.html

下面是代码和图片,因为爬的是一个不太正经的网站(没有反爬),所以把网址隐藏了,不知道会不会影响你研究
图片:

                               
登录/注册后可看大图


  1. import requests
  2. from fake_useragent import UserAgent
  3. import os
  4. import threading
  5. from lxml import etree
  6. from queue import Queue
  7. import time
  8. import re


  9. class Producer(threading.Thread):
  10.     headers = {
  11.         "Referer": "https://www.隐藏.com",
  12.         "User-Agent": UserAgent().random
  13.     }

  14.     def __init__(self, page_queue, img_queue):
  15.         threading.Thread.__init__(self)
  16.         self.page_queue = page_queue
  17.         self.img_queue = img_queue

  18.     def get_url(self, page_url):
  19.         re1 = requests.get(page_url, headers=self.headers)
  20.         e1 = etree.HTML(re1.text)
  21.         title_urls_half = e1.xpath("//div[@class='box list channel']/ul/li/a/@href")
  22.         zhu_url = get_zhuye()
  23.         for title_urls in title_urls_half:
  24.             title_url = zhu_url + title_urls
  25.             re2 = requests.get(title_url, headers=self.headers)
  26.             e2 = etree.HTML(re2.text)
  27.             img_urls = e2.xpath("//div[@class='content']/p/img/@src")
  28.             for img_url in img_urls:
  29.                 self.img_queue.put(img_url)
  30.                 # img_url 就是图片地址

  31.     def run(self):
  32.         while self.page_queue.empty() == False:
  33.             page_url = self.page_queue.get()
  34.             self.get_url(page_url)


  35. class Consumer(threading.Thread):
  36.     headers = {
  37.         "Referer": "https://www.隐藏.com",
  38.         "User-Agent": UserAgent().random
  39.     }

  40.     def __init__(self, page_queue, img_queue):
  41.         threading.Thread.__init__(self)
  42.         self.page_queue = page_queue
  43.         self.img_queue = img_queue

  44.     def run(self):
  45.         while True:
  46.             time.sleep(2)
  47.             if self.img_queue.empty():
  48.                 break
  49.             img_url = self.img_queue.get()
  50.             filename = img_url.split("/")[-1]
  51.             try:
  52.                 response = requests.get(img_url, headers=self.headers, timeout=5)
  53.                 print(img_url)
  54.                 with open(filename, "wb") as f:
  55.                     f.write(response.content)
  56.             except:
  57.                 print("超时")


  58. def get_zhuye():
  59.     headers = {
  60.         "Referer": "https://www.隐藏.com",
  61.         "User-Agent": UserAgent().random
  62.     }
  63.     base_url = "https://www.隐藏.com"
  64.     response = requests.get(base_url, headers=headers)
  65.     zhu_url = re.findall(r'window\.location\.href="(.+?)"', response.text)[0][:-1]
  66.     return zhu_url


  67. def main():
  68.     zhu_url = get_zhuye()
  69.     page_queue = Queue(50)
  70.     img_queue = Queue(1000)

  71.     a = int(input("输入下载开始的页码(≥2):"))
  72.     b = int(input("输入下载结束的页码(≥2):"))
  73.     for x in range(a, b + 1):
  74.         page_url = zhu_url + "/pic/2/index_%d.html" % x
  75.         page_queue.put(page_url)

  76.     for x in range(5):
  77.         t = Producer(page_queue, img_queue)
  78.         t.start()
  79.         # t.join()

  80.     for x in range(5):
  81.         t = Consumer(page_queue, img_queue)
  82.         t.start()
  83.         # t.join()


  84. if __name__ == '__main__':
  85.     images_path = os.path.join(os.path.dirname(__file__), "images")
  86.     if not os.path.exists(images_path):
  87.         os.mkdir(images_path)
  88.     os.chdir(images_path)
  89.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-20 14:57:49 | 显示全部楼层
发你的代码和报错截图
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-20 18:05:56 | 显示全部楼层
suchocolate 发表于 2020-5-20 14:57
发你的代码和报错截图

我发过了竟然还在审核
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-20 19:45:15 | 显示全部楼层
本帖最后由 xmpython 于 2020-5-20 20:32 编辑
suchocolate 发表于 2020-5-20 14:57
发你的代码和报错截图


审核通过了,代码和图,如果需要具体的网址的话我也可以给你

关键是单步调试可以正常下载正常跑,说明程序是正确的,但是一运行就卡住,这才是最神奇最吐血的地方
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-19 20:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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