python爬取知乎文章_selenium+python+BeautifulSoup爬取知乎文章信息
#知乎推荐文章爬取
#2017/8/6
# -*- encoding = utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException
from bs4 import BeautifulSoup
import csv
import os
import time
import re
driver = webdriver.Chrome()
#登录知乎
def putcookies(account,password):
try:
driver.get('https://www.zhihu.com/#signin')
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "body > div.index-main > div > div.desk-front.sign-flow.clearfix.sign-flow-simple > div.index-tab-navs > div > a.active")))
botton = driver.find_element_by_css_selector('body > div.index-main > div > div.desk-front.sign-flow.clearfix.sign-flow-simple > div.view.view-signin > div.qrcode-signin-container > div.qrcode-signin-step1 > div.qrcode-signin-cut-button > span')
botton.click()
form = driver.find_element_by_css_selector('body > div.index-main > div > div.desk-front.sign-flow.clearfix.sign-flow-simple > div.view.view-signin > form > div.group-inputs > div.account.input-wrapper > input[type="text"]')
pas = driver.find_element_by_css_selector('body > div.index-main > div > div.desk-front.sign-flow.clearfix.sign-flow-simple > div.view.view-signin > form > div.group-inputs > div.verification.input-wrapper > input[type="password"]')
sub = driver.find_element_by_css_selector('body > div.index-main > div > div.desk-front.sign-flow.clearfix.sign-flow-simple > div.view.view-signin > form > div.button-wrapper.command > button')
form.send_keys(account)
pas.send_keys(password)
sub.click()
try:
print('请手动输入验证码')
driver.implicitly_wait(10)
driver.find_element_by_css_selector('#root > div > div:nth-child(2) > header > div > div.SearchBar > button')
except NoSuchElementException:
sub.click()
except:
putcookies(account,password)
#滑动页面
def change_page(num):
WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR,'#root > div > div:nth-child(2) > header > div > div.SearchBar > button')))
for i in range(num):
driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
time.sleep(3)
#解析页面
def findinf(html):
soup = BeautifulSoup(html,'lxml')
r = re.compile('(\d+)')
links = soup.find_all('div',class_='Card TopstoryItem')
for link in links:
try:
maininf = link.find(class_='Feed-meta-item').get_text()[-3:]#话题
writer = link.find(class_='AuthorInfo-head').get_text()#作者
except:
continue
try:
intd = link.find('div',class_='RichText AuthorInfo-badgeText').string#作者个人介绍
except:
intd = ''
title = link.find('h2',class_='ContentItem-title').get_text()#标题
href = 'https://www.zhihu.com' + link.find('h2',class_='ContentItem-title').a['href']#文章链接
try:
support = link.find(class_='Button VoteButton VoteButton--up').get_text()#点赞
except:
support = link.find(class_='Button LikeButton ContentItem-action').get_text()#点赞
try:
talking = r.match(link.find('button',class_='Button ContentItem-action Button--plain').get_text()[:-3]).group()#评论数
except:
talking = ''
content = link.find('span',class_='RichText CopyrightRichText-richText').get_text()#摘要
yield {
'maininf': maininf,
'writer':writer,
'intd':intd,
'title':title,
'support':support,
'talking':talking,
'content':content,
'href':href,
}
#创建一个文件夹
def make(path):
if not os.path.exists(path):
os.makedirs(path)
#保存数据
def save_to_csv(inf,path):
with open(path + '知乎文章信息概要采集.csv','a') as f:
writer = csv.writer(f)
writer.writerow(['标题','作者','话题','作者个人介绍','点赞数','评论数','文章链接','摘要'])
try:
for i in inf:
writer.writerow([i['title'],i['writer'],i['maininf'],i['intd'],i['support'],i['talking'],i['href'],i['content']])
except:
pass
#主函数
def main(account,password,num):
path = 'D:/数据/知乎文章/'
putcookies(account,password)
change_page(num)
inf = findinf(driver.page_source)
make(path)
print('---'*43)
print('{:^60}'.format('知乎文章概要'))
print("***"*43)
for i in findinf(driver.page_source):
print('标题:{:<10s}'.format(i['title']))
print('作者:{:>3s}'.format(i['writer']),end = ' '*5)
print("话题:{:>3s}".format(i['maininf']))
print('作者个人介绍:')
print('{:<5s}'.format(i['intd']))
print('点赞数:{:<2s}'.format(i['support']),end = ' '*5)
print("评论数:{:3s}".format(i['talking']))
print("文章链接:" + i['href'])
print("摘要:")
print('{:<5s}'.format(i['content']))
print('---' *43)
save_to_csv(inf,path)
#执行程序
if __name__ == '__main__':
num = int(input('请输入要爬取的页面数:'))
account = input("请输入知乎账号:")
password = input("请输入知乎密码:")
time_start = time.time()
main(account,password,num)
print("^^^"*43)
print("共耗时{}秒".format(time.time()-time_start))
driver.quit()
python爬取知乎文章_selenium+python+BeautifulSoup爬取知乎文章信息相关推荐
- python爬虫:使用selenium、unittest和BeautifulSoup爬取斗鱼tv的当前直播人数
import unittest from selenium import webdriver from bs4 import BeautifulSoup as bsclass douyu(unitte ...
- python接管已经打开ie浏览器_Selenium+Python浏览器调用:IE
IE浏览器调用 IE浏览器驱动添加 这里我用的是IEDriverServer_Win32_2.43.0.zip,下载后解压,把IEDriverServer.exe放在python安装目录,与pytho ...
- python对浏览器的常用操作_selenium+python基本操作(02)
前言 前一章节已经完成环境搭建,下面简单介绍下对浏览器的基本操作,让大家先了解一些简单的操作.这节主要介绍浏览器打开.刷新.前进.后退.截图.退出等功能. 1)打开浏览器 from selenium ...
- python测试用例不通过发送报告_selenium+python自动化89-用例不通过的时候发送邮件...
实现需求:当测试用例全部通过的时候,不发邮件,当用例出现Error或Failure的时候发送邮件 解决思路:生成html测试报告后,用bs4解析html页面,写个函数判断页面上是都有不通过的记录 ht ...
- python爬虫selenium账号和密码_selenium + python 登录页面,输入账号、密码,元素定位问题...
示例简介: 要求:登录QQ邮箱,输入账号.密码 出现问题:页面中含有iframe框架,因此直接进行元素的查找与操作,出现找不到元素的现象,首先需进行iframe框架的转换,使用switch_to_fr ...
- [python应用案例] 一.BeautifulSoup爬取天气信息并发送至QQ邮箱
前面作者写了很多Python系列文章,包括: Python基础知识系列:Python基础知识学习与提升 Python网络爬虫系列:Python爬虫之Selenium+Phantomjs+CasperJ ...
- 【实例】python 使用beautifulSoup 抓取网页正文 以淘宝商品价格为例
参考文章: 利用BeautifulSoup抓取新浪网页新闻的内容 使用Requests库和BeautifulSoup库来爬取网页上需要的文字与图片 -------------------------- ...
- python爬虫知乎图片_Python爬虫入门教程 25-100 知乎文章图片爬取器之一
1. 知乎文章图片爬取器之一写在前面 今天开始尝试爬取一下知乎,看一下这个网站都有什么好玩的内容可以爬取到,可能断断续续会写几篇文章,今天首先爬取最简单的,单一文章的所有回答,爬取这个没有什么难度. ...
- 如何使用python编程抢京东优惠券 知乎_学好Python爬取京东知乎价值数据
原标题:学好Python爬取京东知乎价值数据 Python爬虫为什么受欢迎 如果你仔细观察,就不难发现,懂爬虫.学习爬虫的人越来越多,一方面,互联网可以获取的数据越来越多,另一方面,像 Python这 ...
- 最全知乎专栏合集:爬取11088个知乎专栏,打破发现壁垒(编程、python、爬虫、数据分析..)
最近逛博客,看到一篇很好的文章,整合了知乎上所有优秀的编程.算法专栏,对学习的帮助非常大,转载过来分享给大家: 众所周知,知乎官方没有搜素专栏的功能,于是我通过爬取几十万用户个人主页所关注的专栏从而获 ...
最新文章
- mysql中的输出,mysql-将输出结果作为SQL中的列
- java 输入 方程,用java 编写一程序,求解一元二次方程:aX2+bX+c=0.参数a、b及c从命令行做参数输入 java...
- Javolution 2.2.5 - Java Struct/Union Simplified
- 在Python中使用Twitter Rest API批量搜索和下载推文
- IOS中单例的简单使用
- The Distribution File System
- Leecode刷题热题HOT100(4)——寻找两个正序数组的中位数
- 此博客记录我的进阶之路(PHP、C、Python、Erlang)
- Pytorch permute()的简单用法
- 将职业教育职业化 - 各IT培训中心必须完成的使命
- 分小组 java_蓝桥杯-分小组-java
- 选择排序法之Java实现
- Oracle中INSTR函数,及在DB2、Sybase中与Instr函数功能相同的函数
- NOI题库答案(1.1 编程基础之输入输出)
- 《人生哲理》二.人生苦短,别懂得太晚了...
- 面板数据、工具变量选择和Hausman检验的若干问题
- 适合运动的无线蓝牙耳机有哪些,运动无线蓝牙耳机推荐
- R语言中的函数1:outer(张量积)
- echarts之静态与动态地图
- 4. PCIe 接口时序
热门文章
- 创建Rss Feeds(一)
- luoguP3507 [POI2010]GRA 性质 + 动态规划
- Vue.nextTick DOM 更新循环结束之后执行延迟回调
- Python __getattribute__ vs __getattr__
- lintcode_189. 丢失的第一个正整数
- 浅谈TCP/IP网络编程中socket的行为
- 第四次作业(胡明浩)
- C#虚基类继承与接口的区别
- 02-Swift学习笔记-元组类型
- window.showModalDialog用法