python使用threading模块多线程获取豆瓣书籍url

python使用threading模块多线程获取豆瓣书籍url

代码如下:

import threading
import requests
from lxml import etree

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.69'
}
threads = []
url_detail = []

def url_tiqu(url):
    global url_detail
    wenben = requests.get(url=url, headers=headers).text
    wenben = etree.HTML(wenben)
    url_detail = wenben.xpath('//li[@class="subject-item"]//a[@class="nbg"]/@href')
    print(url_detail)

for i in range(0, 380, 20):
    thread = threading.Thread(target=url_tiqu,
                              args=(f'https://book.douban.com/tag/%E7%AB%A5%E8%AF%9D?start={i}&type=T',))
    thread.start()
print('多线程!启动!')

又水文一篇,下线!睡觉.