BeautifulSoup和Selenium对比
来源:互联网 发布:数据合计js结果显示 编辑:程序博客网 时间:2024/04/29 13:54
参照http://blog.csdn.net/eastmount/article/details/53932775#
BeautifulSoup:处理速度快,同时可以连续查找,主要用于静态网页
经过BeautifulSoup处理以后,编码方式都变成了Unicode,需要将其变成所需的编码方式:可以利用encode(‘需要的编码’),还可以利用
BeautifulSoup(网页/html,”lxml/xml”).prettify(‘需要的编码’)
可以利用soup.original_encoding检测原来的编码
我们想用 class 过滤,不过 class 是 python 的关键词,这怎么办?加个下划线就可以
.find_all(标签,类别)
#! /user/bin/env python#encoding=utf-8__author__ = 'chw'from bs4 import BeautifulSoupimport requestsheaders={'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36'}file = open('250.txt' , 'w')def craw(url): response=requests.get(url,headers=headers) text=response.text soup=BeautifulSoup(text,'lxml') print u'豆瓣电影250: 序号 \t影片名\t 评分 \t评价人数' for tag in soup.find_all("div",***class_***="item"): # num num=tag.find('em').get_text() # 名字 name1=tag.find_all('span')[0].get_text() name2=tag.find_all('span')[1].get_text() name3 = tag.find_all('span')[2].get_text() name=name1+name2+name3 print num+'\t'+name file.write(num+'\t'+name) print u'中文名字:'+name1 file.write(u'中文名字:'+name1) # 分数和评价人数 ping_fen=tag.find('div', class_="star").find_all('span')[1].**get_text**() ping_jia = tag.find('div', class_="star").find_all('span')[3].get_text() print u'评分'+ping_fen print u'评价'+ping_jia file.write(u'评分'+ping_fen) file.write(u'评分' + ping_jia)if __name__=='__main__': for j in xrange(0,10): j=j*25 url='https://movie.douban.com/top250?start='+str(j)+'&filter=' craw(url) file.close()
Selenium:主要用于动态网页,查找速度慢,解析时要注意
.find_**elements**_by_xpath和.find_**element**_by_xpath有区别,同时利用浏览器时要配置。
.PhantomJS:
drive=webdriver.PhantomJS(‘D:\Anaconda2\phantomjswindow**s\bin**phantomjs.exe’)
谷歌浏览器:要下载驱动
#! /user/bin/env python#encoding=utf-8__author__ = 'chw'import requestsfrom selenium import webdriverfrom selenium.webdriver.common.keys import Keysfile=open('200.txt','w')def craw(url): drive.get(url) print u'豆瓣电影250: 序号 \t影片名\t 评分 \t评价人数' content=drive.find_elements_by_xpath("//div[@class='item']") for num in content: print num.***text*** # content=drive.find_elements_by_xpath("//div[@class='item']") # for tag in content: # print tag.textif __name__=='__main__': drive=webdriver.PhantomJS('D:\Anaconda2\phantomjswindows\phantomjs.exe') # drive = webdriver.PhantomJS('C:\Program Files\Google\Chrome\Application\chromedriver.exe') for j in xrange(0,2): j=j*25 url='https://movie.douban.com/top250?start='+str(j)+'&filter=' craw(url) file.close()
0 0
- BeautifulSoup和Selenium对比
- BeautifulSoup和Selenium对比爬取豆瓣Top250电影信息
- [python爬虫] BeautifulSoup和Selenium对比爬取豆瓣Top250电影信息
- selenium 和 PhantomJS,BeautifulSoup 百度知道爬虫实战
- 使用python3抓取新浪股票使用beautifulsoup解析和selenium
- [python爬虫] BeautifulSoup和Selenium简单爬取知网信息测试
- selenium+python+BeautifulSoup爬取知乎文章信息
- BeautifulSoup中的HTML解析器对比
- beautifulsoup介绍和安装
- BeautifulSoup find() 和 find_all()
- BeautifulSoup 用法和实例
- 使用Python+selenium+BeautifulSoup抓取动态网页的关键信息
- Python爬取淘宝搜索页,使用Selenium+BeautifulSoup
- python selenium+beautifulSoup爬取彩票网数据
- BeautifulSoup
- BeautifulSoup
- BeautifulSoup
- beautifulsoup
- 强化学习读书笔记
- Tomcat Sqoop import异常及其解决方法
- 易的核心理念之天道左旋、地道右旋
- 系统集成项目管理工程师教程看书笔记8
- Tomcat部署Servlet出现404解决方案
- BeautifulSoup和Selenium对比
- 今天干了一件很爽的事儿---清理github代码库
- tar 的用法
- ESP8266--学习笔记(四)TCP通信--TCP-Client
- PullToRefreshListView 上拉加载,下拉刷新 + AsyncTask 网络请求 + gson 自动解析
- 蓝桥杯java练习——递归算法细菌增值
- DIV盒子居中问题
- PHP下ereg实现匹配ip的正则
- 斐波那契博弈