国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

爬取今日頭條Ajax請求

瀏覽:291日期:2022-06-11 11:18:07

網(wǎng)址:https://www.toutiao.com/

搜索頭條

可以得到這個網(wǎng)址:

開發(fā)者工具查看:

我們在搜索中并沒有發(fā)現(xiàn)上面的文字,那么我們可以初步判定,這個由Ajax加載,然后渲染出來的。此時切換到xhr過濾,可以看到確實是ajax請求。

觀察請求的特點,發(fā)現(xiàn)只有offset是改變的,而且一次加20,。

我們可以用它來控制數(shù)據(jù)分頁,然后把圖片下載下來。代碼如下:

import requestsimport osfrom urllib.parse import urlencodefrom hashlib import md5from multiprocessing.pool import Poolfrom requests import codesdef get_page(offset):  params = {    "offset":offset,    "format":"json",    "keyword":"街拍",    "autoload":"true",    "count":"20",    "cur_tab":"1",    "from":"search_tab"  }  url = "https://www.toutiao.com/search_content/?"+urlencode(params)  try:    response = requests.get(url)    if response.status_code == 200:      # print(url)      return response.json()  except requests.ConnectionError:    return None# get_page(0)def get_images(json):  if json.get("data"):    for item in json.get("data"):      if item.get("cell_type") is not None:continue      title = item.get("title")      images = item.get("image_list")      for image in images:yield {  "title":title,  "image":"https:" + image.get("url"),}def save_image(item):  #os.path.sep  路徑分隔符‘//"  img_path = "img" + os.path.sep + item.get("title")  if not os.path.exists(img_path):    os.makedirs(img_path)  try:    resp = requests.get(item.get("image"))    # print(type(resp))    if codes.ok == resp.status_code:      file_path = img_path + os.path.sep + "{file_name}.{file_suffix}".format(file_name=md5(resp.content).hexdigest(),#md5是一種加密算法獲取圖片的二進(jìn)制數(shù)據(jù),以二進(jìn)制形式寫入文件file_suffix="jpg")      if not os.path.exists(file_path):with open(file_path,"wb")as f:  f.write(resp.content)  print("Downladed image path is %s" % file_path)      else:print("Already Downloaded",file_path)  except requests.ConnectionError:    print("Failed to Save Image,item %s" % item)def main(offset):  json = get_page(offset)  for item in get_images(json):    print(item)    save_image(item)GROUP = 0GROUP_END = 2if __name__ == "__main__":  pool = Pool()  groups = ([x*20 for x in range(GROUP,GROUP_END)])  pool.map(main,groups)  #將groups一個個調(diào)出來傳給main函數(shù)  pool.close()  pool.join()   #保證子進(jìn)程結(jié)束后再向下執(zhí)行 pool.join(1) 等待一秒

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

標(biāo)簽: Ajax
相關(guān)文章:
主站蜘蛛池模板: 国产精品一级 | 韩国一级毛片视频 | 成人a毛片免费全部播放 | 在线99| 情侣自拍啪啪 | 国产一级成人毛片 | 草草草在线观看 | 亚洲日本久久一区二区va | 亚洲天堂.com| 精品欧美成人高清视频在线观看 | 欧美一级毛片欧美毛片视频 | 欧美特级视频 | 欧美一级日本一级韩国一级 | 日本成人免费在线 | 免费鲁丝片一级观看 | 久久久久久久久国产 | 99福利资源久久福利资源 | 91精品国产91热久久久久福利 | 亚洲综合免费 | 亚洲二区在线播放 | 国产精品久久久久久免费播放 | 午夜手机视频 | 国产一区二区三区高清视频 | 欧美怡红院免费全视频 | 韩国日本三级在线观看 | 国产va免费精品高清在线观看 | 久久欧美精品欧美九久欧美 | 在线观看国产精品日本不卡网 | 色欧美在线| 国产精品国产三级在线高清观看 | 日本天堂网 | 国产一区二区免费播放 | 99精彩视频 | 一级做a爰片久久毛片免费看 | 成人在线免费 | 欧美成人午夜免费完成 | 日本不卡一区二区三区在线观看 | 99福利资源久久福利资源 | 午夜免费69性视频爽爽爽 | 日本在线观看网址 | 日韩美女专区中文字幕 |