当前位置:首页 > 日记本 > 正文内容

python固定线程运行实例

zhangchap3年前 (2021-10-24)日记本239
import requests
from urllib.parse import quote
from threading import  Thread

_headers = {
   'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0',
   # 'Cookie':"BDqhfp=vivo%E6%89%8B%E6%9C%BA%26%26NaN-1undefined%26%260%26%261; BAIDUID=21434EE70F723B9070DA0F473BFA049F:FG=1; BIDUPSID=21434EE70F723B90F29D09B4C3D5C41D; PSTM=1614749848; H_WISE_SIDS=107316_110085_114551_127969_175756_176398_178384_178529_178639_178713_179349_179365_179455_180863_181106_181133_181136_181250_181398_181488_181589_182000_182001_182232_182530_182847_182891_183035_183238_183308_183328_183345_183611_183869_183905_183923_183926_183932_183976_184011_184246_184321_184359_184440_184578_184793_184809…cba4a0b3705a0a7e87c71ef72ee5; uc_recom_mark=cmVjb21tYXJrXzY3Mjc0OTM%3D; BDRCVFR[dG2JNJb_ajR]=mk3SLVN4HKm; userFrom=www.baidu.com; BDRCVFR[-pGxjrCMryR]=mk3SLVN4HKm; BCLID=10370034066868452322; BDSFRCVID=ybDOJexroG0v1_3HtpIOb7qEDrizcSTTDYrEOwXPsp3LGJLVgq_eEG0PtDR-eqP-oxUCogKKBeOTHn0F_2uxOjjg8UtVJeC6EG0Ptf8g0M5; H_BDCLCKID_SF=tRk8oDDafCvDqTrP-trf5DCShUFsh4jRB2Q-XPoO3KJ-hfARybO2yf4VjpQ40nj0MD7g-Ubgy4op8P3y0bb2DUA1y4vpKbod5mTxoUJ23U_VetjMqtnW3ftebPRi3tQ9QgbXopQ7tt5W8ncFbT7l5hKpbt-q0x-jLTnhVn0MBCK0HPonHjLhD6QX3H",
}
def download(word):
   url = f'https://image.baidu.com/search/acjson?tn=resultjson_com&logid=10637821531995368276&ipn=rj&ct=201326592&is=&fp=result&queryWord={quote(word)}&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=&z=&ic=&hd=&latest=&copyright=&word={quote(word)}&s=&se=&tab=&width=&height=&face=&istype=&qc=&nc=1&fr=&expermode=&nojc=&cg=brand&pn=0&rn=60&gsm=1e0000003c&1634957179471='
   try:
      r = requests.get(url,headers = _headers,timeout = 10 )
   except requests.RequestException as err:
      print(err)
      return

   source = r.json()
   print(source.get('bdFmtDispNum','被封了'),'数据量:',len(source.get('data',[])))
   return source

def parse(source:dict):
   data = source.get('data')
   if data is None:
      print(f'获取数据失败{source}')
      return
   for item in data:
      title = item.get('fromPageTitleEnc')
      if title is None:
         continue
      hover_url = item.get('hoverURL')
      if hover_url is None:
         continue
      print(title)
      print(hover_url)
      print('****'*50)


if __name__ == '__main__':
   threads = []
   while 1:
      for t in threads:
         if not t.is_alive():
            threads.remove(t)
      if len(threads) < 30:
         t = Thread(target=download,args=('华为手机',))
         t.start()
         threads.append(t)


分享给朋友:

相关文章

网络编辑工具箱注册ComCtl32.ocx

1.把ComCtl32.ocx放到c:\Windows\SysWOW64,注:需要管理员权限 2.打开C:\Windows\System32 找到 cmd.exe 鼠标右键管理员身份...

lxml win 安装方法

文档地址:https://lxml.de/installation.htmlwindows 下安装不了的都可以在这里找:https://www.lfd.uci.edu/~gohl...

python 随机生成时间戳写入txt文件/运行sql语句

import time from random import randint with open('time.txt', ...

python 获取当前时间及随机时间戳

import time from random import randint time.strftime('%Y-%m-%d %H:%M:...

python url.parse模块编码解码

from urllib.parse import quote,unquote,urlencode # 对汉字进行编码使用 quote ...

python读取txt文件放到Queue队列

from queue import Queue with open('kw.txt',encoding='utf-8')&nb...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。