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、爬虫、数据分析..)
最近逛博客,看到一篇很好的文章,整合了知乎上所有优秀的编程.算法专栏,对学习的帮助非常大,转载过来分享给大家: 众所周知,知乎官方没有搜素专栏的功能,于是我通过爬取几十万用户个人主页所关注的专栏从而获 ...
最新文章
- 经典的printk 写法
- 2.4g和5g要不要合并_2.4 序列之字符串
- asm 查看 数据文件 修改 时间_更高效的GMX分段模拟方法:修改tpr文件
- 区块链BaaS云服务(28)TOP Network 之全分片主链(Layer-1)
- 用FTP客户端实现主机和虚拟机之间文件的传输(方法2)
- Android教程-Java基础2 语法与关系运算
- Activity启动模式 launchMode
- 使用Netbeans开发App Engine Java
- java安全权限_java.security.SecurityPermission
- 设计模式之创建者模式
- Vue入门---- vue-router
- hadoop 集群启动时 Address already in use 解决方法
- 【PyTorch】PixelShuffle
- 为什么jQuery或诸如getElementById之类的DOM方法找不到元素?
- C++箴言:多态基类中将析构函数声明为虚拟zz
- Atitit godaddy 文件权限 root权限设置
- 转录组测序(RNA-seq)详细建库步骤与原理
- 使用Unity3D引擎制作塔防类游戏(一)
- 为什么Wannacry 勒索病毒加密的部分数据能恢复?
- 带你走进神经网络的“前世今生”
热门文章
- Linux操作系统中修改hostname
- Linux操作系统中使用“autogen.sh+configure+make”编译代码的方法
- 关于go语言的测试相关内容笔记
- MapReduce入门(二)合并小文件
- MySql is marked as crashed and should be repaired问题
- 探索WebKit内核(一)------ 菜鸟起步
- TreeView中丢失的图标
- re.split() 根据句子中的序号进行切分
- 基于数值数据理解和重要信息验证的数据到文本生成模型
- 中国成为「研究生大国」,99%研究生背后的焦虑:就业更难了!