色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

爬取今日頭條Ajax請求

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

網址:https://www.toutiao.com/

搜索頭條

可以得到這個網址:

開發者工具查看:

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

觀察請求的特點,發現只有offset是改變的,而且一次加20,。

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

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是一種加密算法獲取圖片的二進制數據,以二進制形式寫入文件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一個個調出來傳給main函數  pool.close()  pool.join()   #保證子進程結束后再向下執行 pool.join(1) 等待一秒

總結

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

標簽: Ajax
相關文章:
主站蜘蛛池模板: 国产成人精品午夜免费 | 日韩中文字幕免费 | 日本欧美国产精品 | 国产成人精品一区二区视频 | 欧美三级欧美成人高清www | 久草视频在线播放 | 欧美精品午夜毛片免费看 | 久久香蕉国产精品一区二区三 | 一本久道久久综合婷婷五 | 国产精品一区二区免费 | 午夜毛片视频高清不卡免费 | 明星国产欧美日韩在线观看 | 99国内精品久久久久久久 | 性欧美videofree另类17 | 欧美成人午夜免费完成 | 欧美成人看片一区二区三区 | 色综合久久久 | 欧美h版成版在线观看 | 日本加勒比在线视频 | 午夜综合 | 日本红怡院亚洲红怡院最新 | 一本色道久久综合网 | 久久久久久全国免费观看 | 免费高清毛片在线播放视频 | 国产一区二区三区视频 | 97视频在线观看免费 | 成人高清 | 视频在线一区二区三区 | 好看毛片| 亚洲一区亚洲二区 | 日韩精品一区二区三区毛片 | 国产香蕉尹人综合在线观 | 色综合亚洲七七久久桃花影院 | 特级毛片免费观看视频 | 一本大道香蕉久在线不卡视频 | 亚洲 欧美 激情 另类 校园 | 中文字幕二区 | 男女扒开双腿猛进入爽爽视频 | 国产精品国产三级国产普通 | 日韩一及片 | 欧美成人免费sss |