爬虫01-百度热点的多种爬取方法

百度热点的多种爬取方法

对比下多个方法,入门之用

selenium爬取

from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.baidu.com')
ele_hots = driver.find_elements('css selector','.title-content-title')
for ele_hot in ele_hots:
    print(ele_hot.text)

pyquery爬取

from pyquery import  PyQuery as pq
from local_fake_useragent import UserAgent
ua_chrome = UserAgent('chrome')
baidu = pq(url='https://www.baidu.com/',headers={'user-agent':ua_chrome.rget})
hot_news = baidu('.title-content-title').items()
for hot in hot_news:
    print(hot.text())

requests+lxml爬取

import requests
from lxml import etree
from local_fake_useragent import UserAgent
ua_chrome = UserAgent('chrome')
baidu_text = requests.get(url='https://www.baidu.com/',headers={'user-agent':ua_chrome.rget}).text
hot_news = etree.HTML(baidu_text).xpath('//*[@class="title-content-title"]')
for hot in hot_news:
    print(hot.text)

对比

方案一方案二方案三
seleniumpyqueryrequests+lxml
UI××
需要ua×
定位方式css|xpathcssxpath
获取数据需要操作无需操作无需操作
绕过鉴权有多种方法需要结合其他库需要结合其他库
  • selenium中的确可以隐藏界面,但在爬取的时候可能会失效
  • selenium在该案例中无法提现优势,但在需要高度鉴权的一些场景中(如验证码),就比较方便,比如结合OCR技术,比如滑动鼠标操作,比如cookie、option的绕过等
  • 但在获取所需数据的时候,纯粹的selenium是需要操作的(比如点击,跳转),而其他的库是不需要的,在效率上差距蛮大的。
  • 这些都只是适合小型爬虫
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wuxianfeng023

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值