python爬取知乎回答
来源:互联网 发布:淘宝联盟怎么找pid 编辑:程序博客网 时间:2024/05/21 06:21
1. 安装库
htmlparser用来解析html。
Beautiful Soup 是一个可以从 HTML 或 XML 文件中提取数据的 Python 库。
pip install beautifulsoup4
Selenium 是浏览器自动化测试框架,使用它来模拟用户操作。
利用 pip 安装 selenium
pip install -U selenium
2. 模拟用户进行滚动和点击操作
使用JS控制滚动条的位置:
window.scrollTo(x,y);
竖向滚动条置底
window.scrollTo(0,document.body.scrollHeight) time.sleep(2)
向下滑动后延迟两毫秒等待页面加载。
在页面上通过审查,找到查看更多回答的html代码
<button class="Button QuestionMainAction"type="button">查看更多回答</button>
通过
driver.find_element_by_css_selector('button.QuestionMainAction').click()
来选中并点击这个按钮。
3. html文件结构化
将html文件结构化并保存,原页面的html解析并存储下来
通过prettify()将html结构化,之后存储在本地的txt文件中。
4. 保存并下载图片
注意我们的目的,就是爬取回答下的图片,其他的都不需要。
还是右键审查,可以发现每张图片上面都有的node,没错,这里面存有图片的高清URL和缩略图URL。
每个元素都被html entity编码了,所以我们要将其解码如下。
html.parser.unescape
之后就可以将图片URL保存下来。
最后下载图片。
urllib.request.urlretrieve
5. 结果展示
6. 代码
from selenium import webdriverimport timeimport urllib.requestfrom bs4 import BeautifulSoupimport html.parserdef main(): driver = webdriver.Chrome() # 打开浏览器 driver.get("https://www.zhihu.com/question/40273344") # 打开想要爬取的知乎页面 # 模拟用户操作 def execute_times(times): for i in range(times): driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") time.sleep(2) try: driver.find_element_by_css_selector('button.QuestionMainAction').click() print("page" + str(i)) time.sleep(1) except: break execute_times(5) result_raw = driver.page_source # 这是原网页 HTML 信息 result_soup = BeautifulSoup(result_raw, 'html.parser')# 然后将其解析 result_bf = result_soup.prettify() # 结构化原 HTML 文件 with open("./output/rawfile/raw_result.txt", 'w',encoding="utf-8") as girls: # 存储路径里的文件夹需要事先创建。 girls.write(result_bf) girls.close() print("爬取回答页面成功!!!") with open("./output/rawfile/noscript_meta.txt", 'wb') as noscript_meta: noscript_nodes = result_soup.find_all('noscript') # 找到所有<noscript>node noscript_inner_all = "" for noscript in noscript_nodes: noscript_inner = noscript.get_text() # 获取<noscript>node内部内容 noscript_inner_all += noscript_inner + "\n" noscript_all = html.parser.unescape(noscript_inner_all).encode('utf-8') # 将内部内容转码并存储 noscript_meta.write(noscript_all) noscript_meta.close() print("爬取noscript标签成功!!!") img_soup = BeautifulSoup(noscript_all, 'html.parser') img_nodes = img_soup.find_all('img') with open("./output/rawfile/img_meta.txt", 'w') as img_meta: count = 0 for img in img_nodes: if img.get('src') is not None: img_url = img.get('src') line = str(count) + "\t" + img_url + "\n" img_meta.write(line) urllib.request.urlretrieve(img_url, "./output/image/" + str(count) + ".jpg") # 一个一个下载图片 count += 1 img_meta.close() print("图片下载成功")if __name__ == '__main__': main()
阅读全文
0 0
- python爬取知乎回答
- Python问题回答
- Python爬取知乎回答信息碰到:Max retries exceeded with URL
- 回答
- 回答
- 回答
- [Python 爬虫之路4] 使用selenium爬取知乎任意一个问题下,所有回答中的图片
- python 知乎回答按赞排序
- Python中yield关键字(stackoverflow高票回答)
- 回答python贴吧"python循环10亿次你们花了多长时间?"的问题
- python爬虫beta版之抓取知乎单页面回答(low 逼版)
- window下python3里pip安装mysql-python失败问题回答解决
- 爬取知乎话题下回答,并制作关键字词云
- 提问回答
- 五秒内回答
- 面试回答
- 回答几个问题
- 面试 回答
- SQl Server 添加字段、修改字段
- [undefined,1] 和 [,1]的区别在哪里--认识js中的稀疏数组
- 说说IO(四)- 文件系统
- 字符编码ANSI和ASCII区别、Unicode和UTF-8区别
- RMQ算法浅析
- python爬取知乎回答
- Opencv C++下的Mat归一化
- 一个比较完整的Inno Setup 安装脚本
- mysql 压缩包解压后找不到data文件夹解决办法
- maven managed dependencies找不到maven管理依赖的原因及解决方法
- impala-kudu相关问题经验总结
- UI 一一 UIApplication基本介绍
- XGBoost 与 Boosted Tree
- 如何检测Android设备中的传感器