[Asm] 纯文本查看 复制代码 import requests
from scrapy import Selector
from concurrent.futures import ThreadPoolExecutor
from queue import Queue
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36'
}
class BoundedThreadPoolExecutor(ThreadPoolExecutor):
def __init__(self, max_workers=None, thread_name_prefix=''):
super().__init__(max_workers, thread_name_prefix)
self._work_queue = Queue(max_workers * 2)
def get_href():
# 获取所有url
hrefs = []
url = 'https://www.2717.com/ent/meinvtupian/list_11_1.html'
res = requests.get(url=url, headers=headers)
res.encoding = 'gb2312' # respose重新编码
html = res.text
# 获取目录页所有链接
sel = Selector(text=html)
lis = sel.xpath("//div[@class='MeinvTuPianBox']//li")
for li in lis:
a = li.xpath('.//a')[0]
href = a.css('::attr(href)').extract()[0]
href = 'https://www.2717.com' + href
hrefs.append(href)
return hrefs
def parser_img(href):
print(href)
if __name__ == '__main__':
hrefs = get_href()
executor = BoundedThreadPoolExecutor(max_workers=8)
for href in hrefs:
executor.submit(parser_img, href) |