【转】BeautifulSoup和Selenium对比

参照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 BeautifulSoup
import requests
headers={
'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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

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 requests
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
file=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.text
if __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()
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值