day94_11_26爬虫find与findall
一。使用json
正常的,如果需要将response结果序列化,需要将结果json.loads
res1=json.loads(response.text)
但是这样会很麻烦,request提供了json方法:
res2=response.json() #直接获取json数据
二。SSL认证
ssl就是http+SSL,也就是https。需要带上证书才能访问特定的网站。
证书需要浏览器下载。
#SSL
# https=http+ssl
import requests
respone=requests.get('https://www.12306.cn',
cert=('/path/server.crt',
'/path/key'))
print(respone.status_code)
三。使用代理
在get请求中proxies关键字就是存放代理网址,:(西刺)
通过META.get('REMOVE_ADDR')
import reques1ts
proxies={
'http':'http://egon:123@localhost:9743',#带用户名密码的代理,@符号前是用户名与密码
'http':'http://localhost:9743',
'https':'https://localhost:9743',
'http':'http://124.205.155.148:9090'
}
respone=requests.get('https://www.12306.cn',
proxies=proxies) print(respone.status_code)
四。超时设置
import requests
respone=requests.get('https://www.baidu.com',
timeout=0.0001)
五。 上传文件。
import requests
files={'file':open('a.jpg','rb')}
respone=requests.post('http://httpbin.org/post',files=files)
print(respone.status_code)
另外有检测服务器压力的工具
jmter 压力测试工具
六。使用bs4
使用插件bs4,可以快速匹配页面中的元素。
首先需要下载bs4和lxml
pip install lxml pip install html5lib pip install beautifulsoup
使用时首先需要将数据爬取,并生成Beautiful对象
import requests
from bs4 import BeautifulSoup
url='https://www.autohome.com.cn/news/1/#liststart'
res=requests.get(url)
soup=BeautifulSoup(res.text,'lxml')
再者使用基本用法find,获取一个对象,其中的筛选条件是系与id,name等例子:
div=soup.find(id='auto-channel-lazyload-article')
ul=div.find(name='ul')
li_list=ul.find_all(name='li')
# print(len(li_list))
for li in li_list:
h3=li.find(name='h3')
if h3:
title=h3.text #把h3标签的text取出来
print(title)
a=li.find(name='a')
if a:
article_url=a.get('href') #取出a标签的href属性
print(article_url) img=li.find(name='img')
if img:
img_url=img.get('src')
print(img_url)
p=li.find(name='p')
if p:
content=p.text
print(content)
findall则是将所有元素都找到。
总结:
find:
-name="标签名" 标签
-id,class_,="" 把这个标签拿出来
-标签.text 取标签的内容
-标签.get(属性名) 取标签属性的内容
find_all
其他用法:
from bs4 import BeautifulSoup
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body> <p class="title" id="bbaa"><b name="xx" age="18">The Dormouse's story</b><b>xxxx</b></p>
<p class="xxx" a="xxx">asdfasdf</p> <p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p> <p class="story">...</p>
"""
soup=BeautifulSoup(html_doc,'lxml')
ress=soup.prettify() #美化一下
soup=BeautifulSoup(ress,'lxml')
通过该对象点标签可以直接对其进行操作:
#遍历文档树
# print(soup.p.name) # 获取该对象中的标签名字
# print(soup.p.attrs) # 获取该对象中的属性集合
# print(soup.p.string) # 获取标签中的字
# print(list(soup.p.strings)) # 迭代器
# print(soup.p.text) # 所有
# print(soup.p.b)
# print(soup.body.p.text) # 只识别文本呢
# print(soup.body.p.contents) #生成期中的所有元素
# print(list(soup.body.p.children)) # 迭代器生成期中的所有元素
# print(list(soup.body.p.descendants)) # 迭代器输出所有孩子
# print(soup.body.p.parent) # 输出p的父标签所有的元素
# print(list(soup.body.p.parents)) # 取出所以有父节点
# print(len(list(soup.body.p.parents)))
# print(soup.body.p.previous_sibling) # 他的上一个兄弟
# print(soup.body.p.previous_sibling)
# print(soup.find(class_="xxx").previous_sibling)
# print(soup.p.next_sibling) # 下一个兄弟
# print(soup.a.previous_sibling)
# print(type(soup.p))
查找文档法:
一共有五种过滤器:字符串,正则,布尔,方法,列表
1.通过字符串过滤:
# print(soup.find_all(name='b'))
2.通过正则过滤
# print(soup.find_all(name=re.compile('^b')))
# print(soup.find_all(id=re.compile('^b')))
3.通过列表与布尔值:
# print(soup.find_all(name=['a','b']))
# print(soup.find_all(name=True))
4.通过方法:
# def has_class_but_no_id(tag):
# return tag.has_attr('class') and not tag.has_attr('id')
# print(soup.find_all(name=has_class_but_no_id))
css选择器法:
# print(soup.select(".title"))
# print(soup.select("#bbaa"))
# print(soup.select('#bbaa b')[0].attrs.get('name'))
其他用法:
#recursive=False 只找同一层
#limit 找到第几个之后停止
七。通过测试软件自动点网站。
1.安装selenium模块:
pip install selenium
2.安装插件到项目文件夹下或者puthon下的scripts中,需要核对版本信息。:
http://npm.taobao.org/mirrors/chromedriver/78.0.3904.105/
使用:
from selenium import webdriver
bro=webdriver.Chrome()
bro.get('https://www.baidu.com')
time.sleep(3)
bro.close()
无窗口操作:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys #键盘按键操作
import time from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('window-size=1920x3000') #指定浏览器分辨率
chrome_options.add_argument('--disable-gpu') #谷歌文档提到需要加上这个属性来规避bug
chrome_options.add_argument('--hide-scrollbars') #隐藏滚动条, 应对一些特殊页面
chrome_options.add_argument('blink-settings=imagesEnabled=false') #不加载图片, 提升速度
chrome_options.add_argument('--headless') #浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败
chrome_options.binary_location = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" #手动指定使
bro=webdriver.PhantomJS() bro=webdriver.Chrome(chrome_options=chrome_options)
bro=webdriver.Chrome()
bro.get('https://www.baidu.com')
chrome支持无窗口操作。
自动化控制窗口:
bro=webdriver.Chrome()
bro.get('https://www.baidu.com') # print(bro.page_source)
# time.sleep(3)
time.sleep(1)
#取到输入框
inp=bro.find_element_by_id('kw')
#往框里写字
inp.send_keys("美女")
inp.send_keys(Keys.ENTER) #输入回车
#另一种方式,取出按钮,点击su
time.sleep(3)
bro.close()
day94_11_26爬虫find与findall的更多相关文章
- Python爬虫教程-19-数据提取-正则表达式(re)
本篇主页内容:match的基本使用,search的基本使用,findall,finditer的基本使用,匹配中文,贪婪与非贪婪模式 Python爬虫教程-19-数据提取-正则表达式(re) 正则表达式 ...
- python爬虫--数据解析
数据解析 什么是数据解析及作用 概念:就是将一组数据中的局部数据进行提取 作用:来实现聚焦爬虫 数据解析的通用原理 标签定位 取文本或者属性 正则解析 正则回顾 单字符: . : 除换行以外所有字符 ...
- python爬虫笔记之re.match匹配,与search、findall区别
为什么re.match匹配不到?re.match匹配规则怎样?(捕一下seo) re.match(pattern, string[, flags]) pattern为匹配规则,即输入正则表达式. st ...
- 爬虫常用正则、re.findall 使用
爬虫常用正则 爬虫经常用到的一些正则,这可以帮助我们更好地处理字符. 正则符 单字符 . : 除换行以外所有字符 [] :[aoe] [a-w] 匹配集合中任意一个字符 \d :数字 [0-9] \D ...
- 网络爬虫re模块的findall()函数
findall()函数匹配所有符合规律的内容,并以列表的形式返回结果. a = '"<div>指数' \ '</div>"' word = re.finda ...
- python爬虫笔记之re.compile.findall()
re.compile.findall原理是理解了,但输出不大理解(主要是加了正则表达式的括号分组) 一开始不懂括号的分组及捕捉,看了网上这个例子(如下),然而好像还是说不清楚这个括号的规律(还是说我没 ...
- python获取ip代理列表爬虫
最近练习写爬虫,本来爬几张mm图做测试,可是爬到几十张的时候就会返回403错误,这是被网站服务器发现了,把我给屏蔽了. 因此需要使用代理IP.为了方便以后使用,我打算先写一个自动爬取ip代理的爬虫,正 ...
- 学习日记-从爬虫到接口到APP
最近都在复习J2E,多学习一些东西肯定是好的,而且现在移动开发工作都不好找了,有工作就推荐一下小弟呗,广州佛山地区,谢谢了. 这篇博客要做的效果很简单,就是把我博客的第一页每个条目显示在APP上,条目 ...
- Python初学者之网络爬虫(二)
声明:本文内容和涉及到的代码仅限于个人学习,任何人不得作为商业用途.转载请附上此文章地址 本篇文章Python初学者之网络爬虫的继续,最新代码已提交到https://github.com/octans ...
随机推荐
- 痞子衡嵌入式:飞思卡尔i.MX RTyyyy系列MCU启动那些事(1)- Boot简介
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是飞思卡尔i.MX RTyyyy系列MCU的BootROM功能简介. 截止目前为止i.MX RTyyyy系列已公布的芯片有三款i.MXRT ...
- 为Dynamics 365 USD设置打开调试面板的自定义快捷键
我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...
- 表单生成器(Form Builder)之mongodb表单数据查询——关联查询
这一篇接着记录一下查询相关的操作.想象一下,如果想要在一张表格中展示某些车辆的耗损和营收情况,我们该怎么处理.车辆.耗损.营收各自存储在一张表中,耗损和营收中冗余了车辆信息……我们便想到了关联查询.m ...
- cURL无法访问TLS网站故障解决
大多数人都厌烦使用老旧的系统,无论软件还是硬件.但有的时候又不得不困守其中,坚持延续着系统的寿命,或者还需要点几柱香,祈求神佛的护佑. Linux是一个模块化极好的操作系统,得益于此,当其中有组件落伍 ...
- 基于V7的emWin多屏显示方案模板,同时驱动LCD和OLED例程
说明: 1.多屏驱动跟多图层驱动是类似的,可以使用函数GUI_SelectLayer做切换选择. 2.为了避免OLED闪烁问题,创建一个128*64bit的显存空间,然后使用emWin的GUI_TIM ...
- UNIX系统编程知识点总结——思维导图
根据考纲整理了一下本学期系统编程课的知识点,并且做成了思维导图便于理解和记忆 主要都是一些比较入门的知识点,UNIX博大精深,每一个知识点其实都能深挖 因为博客园不可能直接出思维导图,本文的主要内容都 ...
- 如何使用SpringBoot封装自己的Starter
作者:Sans_ juejin.im/post/5cb880c2f265da03981fc031 一.说明 我们在使用SpringBoot的时候常常要引入一些Starter,例如spring-boot ...
- Scrapy框架-爬虫程序相关属性和方法汇总
一.爬虫项目类相关属性 name:爬虫任务的名称 allowed_domains:允许访问的网站 start_urls: 如果没有指定url,就从该列表中读取url来生成第一个请求 custom_se ...
- 爬虫最新的库requests-html库总结
requests-html是比较新的爬虫库,作者和requests是同一个作者 一.安装依赖 pip install requests-html 我们可以在安装的时候看到他安装了lxml,reuqes ...
- java 超详细面经整理(持续更新)2019.12.18
目录 Java SE 请你谈谈Java中是如何支持正则表达式操作的? 请你简单描述一下正则表达式及其用途. 请你比较一下Java和JavaSciprt? 在Java中如何跳出当前的多重嵌套循环? 讲讲 ...