经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » AJAX » 查看文章
爬取今日头条Ajax请求
来源:jb51  时间:2018/10/20 16:28:28  对本文有异议

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

搜索头条

可以得到这个网址:

https://www.toutiao.com/search/?keyword=%E8%A1%97%E6%8B%8D

开发者工具查看:

我们在搜索中并没有发现上面的文字,那么我们可以初步判定,这个由Ajax加载,然后渲染出来的。此时切换到xhr过滤,可以看到确实是ajax请求。

观察请求的特点,发现只有offset是改变的,而且一次加20,。

我们可以用它来控制数据分页,然后把图片下载下来。代码如下:

  1. import requests
  2. import os
  3. from urllib.parse import urlencode
  4. from hashlib import md5
  5. from multiprocessing.pool import Pool
  6. from requests import codes
  7. def get_page(offset):
  8. params = {
  9. "offset":offset,
  10. "format":"json",
  11. "keyword":"街拍",
  12. "autoload":"true",
  13. "count":"20",
  14. "cur_tab":"1",
  15. "from":"search_tab"
  16. }
  17. url = 'https://www.toutiao.com/search_content/?'+urlencode(params)
  18. try:
  19. response = requests.get(url)
  20. if response.status_code == 200:
  21. # print(url)
  22. return response.json()
  23. except requests.ConnectionError:
  24. return None
  25. # get_page(0)
  26. def get_images(json):
  27. if json.get('data'):
  28. for item in json.get('data'):
  29. if item.get('cell_type') is not None:
  30. continue
  31. title = item.get('title')
  32. images = item.get('image_list')
  33. for image in images:
  34. yield {
  35. 'title':title,
  36. 'image':'https:' + image.get('url'),
  37. }
  38. def save_image(item):
  39. #os.path.sep 路径分隔符‘//'
  40. img_path = 'img' + os.path.sep + item.get('title')
  41. if not os.path.exists(img_path):
  42. os.makedirs(img_path)
  43. try:
  44. resp = requests.get(item.get('image'))
  45. # print(type(resp))
  46. if codes.ok == resp.status_code:
  47. file_path = img_path + os.path.sep + '{file_name}.{file_suffix}'.format(
  48. file_name=md5(resp.content).hexdigest(),#md5是一种加密算法获取图片的二进制数据,以二进制形式写入文件
  49. file_suffix='jpg')
  50. if not os.path.exists(file_path):
  51. with open(file_path,'wb')as f:
  52. f.write(resp.content)
  53. print('Downladed image path is %s' % file_path)
  54. else:
  55. print('Already Downloaded',file_path)
  56. except requests.ConnectionError:
  57. print('Failed to Save Image,item %s' % item)
  58. def main(offset):
  59. json = get_page(offset)
  60. for item in get_images(json):
  61. print(item)
  62. save_image(item)
  63. GROUP = 0
  64. GROUP_END = 2
  65. if __name__ == '__main__':
  66. pool = Pool()
  67. groups = ([x*20 for x in range(GROUP,GROUP_END)])
  68. pool.map(main,groups) #将groups一个个调出来传给main函数
  69. pool.close()
  70. pool.join() #保证子进程结束后再向下执行 pool.join(1) 等待一秒

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对w3xue的支持。如果你想了解更多相关内容请查看下面相关链接

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号