学习链接:https://zhuanlan.zhihu.com/p/28680797

主要呢是用了chrome driver去模拟滚动和点击

同样是用了beautiSoup去解析html文件

解析出图片的url之后转码之后下载图片

代码:

 1 # -*- coding:utf-8 -*-
 2
 3 import sys
 4 import time
 5 import urllib
 6 from bs4 import BeautifulSoup
 7 from HTMLParser import HTMLParser
 8 from selenium import webdriver
 9
10 reload(sys)
11 sys.setdefaultencoding("utf-8")
12
13 def main():
14
15     #用chrome driver打开页面
16     driver = webdriver.Chrome()
17     driver.get("https://www.zhihu.com/question/54104076")
18
19     #用driver进行点击操作并模拟点击查看更多
20     def execute_times(times):
21
22         for i in range(times + 1):
23             driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
24             time.sleep(2)
25             try:
26                 driver.find_element_by_css_selector('button.QuestionMainAction').click()
27                 print "page"+str(i)
28                 time.sleep(1)
29             except :
30                 break
31     execute_times(3) #打开三个页面
32
33     #结构化HTML文件并用beautifulSoup解析
34     result_raw=driver.page_source
35     result_soup = BeautifulSoup(result_raw, 'html.parser')
36     result_bf = result_soup.prettify()
37
38     #找到所有图片
39     with open("D:\\imagine\\raw_result.txt", 'w') as girls:
40         girls.write(result_bf)
41     girls.close()
42     print "Store raw data successfully!!!"
43
44     #找到所有<nonscript>标签并记录下来
45     with open("D:\\imagine\\noscript_meta.txt", 'w') as noscript_meta:
46         noscript_nodes = result_soup.find_all('noscript')
47         noscript_inner_all = ""
48         for noscript in noscript_nodes:
49             noscript_inner = noscript.get_text()
50             noscript_inner_all += noscript_inner + "\n"
51
52         h = HTMLParser()
53         noscript_all = h.unescape(noscript_inner_all)
54         noscript_meta.write(noscript_all)
55     noscript_meta.close()
56     print "Store noscript meta data successfully!!!" #成功找到图片
57
58     #开始下载图片
59     img_soup = BeautifulSoup(noscript_all, 'html.parser')
60     img_nodes = img_soup.find_all('img')
61     with open("D:\\imagine\\img_meta.txt", 'w') as img_meta:
62         count = 0
63         for img in img_nodes:
64             if img.get('src') is not None:
65                 img_url = img.get('src')
66
67                 line = str(count) + "\t" + img_url + "\n"
68                 img_meta.write(line)
69                 urllib.urlretrieve(img_url, "D:\\imagine\\" + str(count) + ".jpg") #下载图片
70                 count += 1
71
72     img_meta.close()
73     print "Store meta data and images successfully!!!" #成功存储图片
74
75 if __name__ == '__main__':
76     main()

结果图:

转载于:https://www.cnblogs.com/general10/p/7530388.html

python之知乎的正确打开方式相关推荐

  1. 【Python】NumPy 中 ravel() 正确打开方式

    [Python]NumPy 中 ravel() 正确打开方式 最近在看书时,对numpy的ravel()的有些疑惑,书上这样写到"如果结果中的值在原始数组中是连续的,则ravel 不会生成底 ...

  2. python batch normalization_Batch Normalization的正确打开方式

    Batch Normalization在TensorFlow中有三个接口调用 (不包括slim.Keras模块中的),分别是: 通过观察这三个接口的参数列表可以得到一个初步的结论,tf.layers. ...

  3. log python_基于Python log 的正确打开方式

    保存代码到文件:logger.py import os import logbook from logbook.more import ColorizedStderrHandler import sm ...

  4. python四舍五入round_四舍五入就用round( )?Python四舍五入的正确打开方式!

    四舍五入就用round( )?Python四舍五入的正确打开方式! 2018-09-22 21:40 阅读数 4 <>round( )函数简介 菜鸟教程中介绍到,round() 函数作用就 ...

  5. Python Matplotlib绘图的正确打开方式

    Python Matplotlib绘图的正确打开方式 文章目录 Python Matplotlib绘图的正确打开方式 1.先搞懂fig.axes.axis `Figure` `Axes` `Axis` ...

  6. python论文参考文献名称_Word的正确打开方式(附毕业论文模板)

       ----点击蓝字关注我呀---- 三年前的我搞毕业设计,第一次接触这玩意儿,一脸懵逼 好在我朱哥搞过大创(还是国家级的),当时给我各种科普单片机的知识 搞大创的好处就是当我们不知道是画机械图还是 ...

  7. opengl 贴图坐标控制_材质贴图正确打开方式

    哈喽,各位观众朋友们好鸭~欢迎来到讲道理画图的地方,我是黄玮宁. 最近呀经常有小伙伴来问我那些不同通道的材质贴图该怎么用,而且频率不是一般的高,所以我觉得有必要来说说这些通道贴图的用法了. 视频版(B ...

  8. pytorch单机多卡的正确打开方式 以及可能会遇到的问题和相应的解决方法

    pytorch 单机多卡的正确打开方式 pytorch 使用单机多卡,大体上有两种方式 简单方便的 torch.nn.DataParallel(很 low,但是真的很简单很友好) 使用 torch.d ...

  9. [分布式训练] 单机多卡的正确打开方式:Horovod

    [分布式训练] 单机多卡的正确打开方式:Horovod 转自:https://fyubang.com/2019/07/26/distributed-training4/ 讲完了单机多卡的分布式训练的理 ...

最新文章

  1. 我用YOLOX露了一手,记录一下模型部署、优化及训练的实现全过程
  2. iOS UI基础-6.0 UIActionSheet的使用
  3. PHP扩展开发(3)-config.m4
  4. [poj1742]coin
  5. SaaS行业乘风杨帆,中小企业市场潜力分析
  6. Modbus RTU CRC校验码计算方法
  7. 利用JS代码完成动态生成表格案例及解析
  8. Kmeans算法实现
  9. igraph 利用节点列表输出子图并存储
  10. WinInet使用详解
  11. 2021年建筑架子工(建筑特殊工种)考试题库及建筑架子工(建筑特殊工种)模拟试题
  12. AccuWeather天气预报应用介绍
  13. 视频编解码——视频编解码器工作原理
  14. 手动挡五个档位示意图_手动挡汽车档位图解?
  15. PHP中使用SHA256,如何在php5.3.0中使用sha256
  16. 【NA】Householder变换
  17. 循环数142857问题 java_循环小数问题
  18. 在钉钉小程序中使用可视化图表插件F2
  19. 如果5秒后地球没有水了,你会怎么样?丨钛空舱
  20. 阿里大鱼短信Spring Boot 版

热门文章

  1. Apache Subversion command line tools下载地址 svn命令行客户端
  2. 【网址收藏】k8s中helm方式安装postgresql及pgadmin
  3. golang变量定义细节及beego环境搭建细节记录
  4. Windows杀死结束进程图形化界面操作方法
  5. Linux shell文件测试运算符
  6. JDBC的CRUD操作之PreparedStatement的删除操作
  7. springboot支持三种嵌入式servlet容器:tomcat(默认),jetty,undertow
  8. python笔试编程题_python_编程面试题
  9. 记录gitee再次提交和错误提示
  10. 直接学python行不行_是否可以直接学python或者java而不学c++?