python的爬虫相关模块使用

BeautifulSoup的使用

  • 首先安装BeautifulSoup
    pip install beautifulsoup4

  • BeautifulSoup默认支持Python的标准HTML解析库,但是它也支持一些第三方的解析库:

序号解析库使用方法优势劣势
1Python标准库BeautifulSoup(html,’html.parser’)Python内置标准库;执行速度快容错能力较差
2lxml HTML解析库BeautifulSoup(html,’lxml’)速度快;容错能力强需要安装,需要C语言库
3lxml XML解析库BeautifulSoup(html,[‘lxml’,’xml’])速度快;容错能力强;支持XML格式需要C语言库
4htm5lib解析库BeautifulSoup(html,’htm5llib’)以浏览器方式解析,最好的容错性速度慢

- 导入库:
from bs4 import BeautifulSoup

  • 下面是简单的例子
import requests
from bs4 import BeautifulSoup
url = "http://www.baidu.com"

session = requests.session()
res = session.get(url=url)
res.encoding = res.apparent_encoding
html_doc = res.text
# print(html_doc)
# print(type(html_doc))

soup = BeautifulSoup(html_doc,'html.parser')

#格式化输出内容:
print(soup.prettify())
  • BeautifulSoup将复杂的html文档转换为树形结构,每一个节点都是一个对象,这些对象可以归纳为几种:

  • Tag
    print(soup.title)
    输出结果
    百度一下,你就知道

  • find_all()方法:
    find_all( name , attrs , recursive , text , **kwargs )
    name参数
    name参数可以查找所有名字为name的Tag,字符串对象自动忽略掉。
    print(soup.find_all('a'))

  • kwyowrds关键字
    查找id是css的
    print(soup.find_all(id='css'))

  • text参数
    用来搜索文档中的字符串内容,text参数也接收字符串、正则表达式、列表、True等参数。
    print(soup.find_all(text=re.compile('^abc')))

Selenium的使用

selenium 是一套完整的web应用程序测试系统,包含了测试的录制(selenium IDE),编写及运行(Selenium Remote Control)和测试的并行处理(Selenium Grid)。Selenium的核心Selenium Core基于JsUnit,完全由JavaScript编写,因此可以用于任何支持JavaScript的浏览器上。selenium可以模拟真实浏览器,自动化测试工具,支持多种浏览器,爬虫中主要用来解决JavaScript渲染问题。

  • 编写简单的程序自动打开指定页面
import os

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

chromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
driver.get("http://www.python.org")
time.sleep(10)
driver.quit()
  • 常用的查找元素的方法
    find_element_by_name
    find_element_by_id
    find_element_by_xpath
    find_element_by_link_text
    find_element_by_partial_link_text
    find_element_by_tag_name
    find_element_by_class_name
    find_element_by_css_selector

  • 访问网站,输入用户名和密码,登录购物网站

import random
import time

import os

from selenium import webdriver


def randomSleep(minS, maxS):
    time.sleep((maxS-minS)*random.random() + minS)


url = 'https://passport.jd.com/new/login.aspx?ReturnUrl=https%3A%2F%2Fwww.jd.com%2F'
chromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
driver.get(url)
randomSleep(1, 3)
driver.find_elements_by_xpath('//a[@clstag="pageclick|keycount|login_pc_201804112|10"]')[0].click()

randomSleep(1, 2)
driver.find_element_by_id('loginname').clear()
randomSleep(1, 3)
driver.find_element_by_id('loginname').send_keys("**************")
randomSleep(1, 2)
driver.find_element_by_id('nloginpwd').send_keys("*****")

randomSleep(3, 5)
driver.find_element_by_id('loginsubmit').click()
randomSleep(5, 10)

print(driver.get_cookies())

driver.close()
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值