lxml

Python爬虫——建立IP代理池

一笑奈何 提交于 2020-04-07 07:42:00
在使用Python爬虫时,经常遇见具有反爬机制的网站。我们可以通过伪装headers来爬取,但是网站还是可以获取你的ip,从而禁掉你的ip来阻止爬取信息。 在request方法中,我们可以通过proxies参数来伪装我们的ip,一些网站上有免费的ip代理网站,可以通过爬取这些ip,经检测后建立ip代理池。 ip代理网站: ( https://www.xicidaili.com/nt/ ) ( https://www.kuaidaili.com/free/intr/ ) 推荐一种常用的伪装头方法 from fake_useragent import UserAgent ua = UserAgent ( ) headers = { 'User-Agent' : ua . random } 接下来进入正题 爬取ip(IPPool.py) import requests from lxml import etree from fake_useragent import UserAgent #伪装 ua = UserAgent ( ) headers = { 'User-Agent' : ua . random } def get_ip ( ) : ip_list = [ ] #路径 url = 'https://www.xicidaili.com/nt/' #ip是有时效的,只爬取第一页

爬虫之爬汽车之家

孤人 提交于 2020-03-29 00:55:21
一、话说爬虫   先说说爬虫,爬虫常被用来抓取特定网站网页的HTML数据,定位在后端数据的获取,而对于网站而言,爬虫给网站带来流量的同时,一些设计不好的爬虫由于爬得太猛,导致给网站来带很大的负担,当然再加上一些网站并不希望被爬取,所以就出现了许许多多的反爬技术。 二、 安装 模块 1. requests 模块安装方法: pip3 install requests 2、beautisoup模块 软件安装方法: pip3 install beautifulsoup4 或 pip3 install bs4 3、lxml模块 #必须先安装whell依赖 (请换成国内pip源进行安装,否则容易报错)pip install wheel #在cmd中,输入python进入python。 然后输入import pip;print(pip.pep425tags.get_supported()),界面上输出当前python的版本信息,如图。 再跟据上面查到的版本信息,找到下面对应的版本进行安装。 #下载地址:https://pypi.python.org/pypi/lxml/3.7.3 (网站打不开,请翻墙,就可以打开)#python3.5就选择cp3m版本 lxml-3.7.3-cp35-cp35m-win32.whl#安装方法pip3 install lxml-3.6.4-cp35-cp35m

知乎大神用Python爬取高颜值美女(爬虫+人脸检测+颜值检测)

荒凉一梦 提交于 2020-03-28 14:15:46
1 数据源 知乎话题『美女』下所有问题中回答所出现的图片 2 抓取工具 Python 3,并使用第三方库 Requests、lxml、AipFace,代码共 100 + 行 3 必要环境 Mac / Linux / Windows (Linux 没测过,理论上可以。Windows 之前较多反应出现异常,后查是 windows 对本地文件名中的字符做了限制,已使用正则过滤) 无需登录知乎(即无需提供知乎帐号密码) 人脸检测服务需要一个百度云帐号(即百度网盘 / 贴吧帐号) 4 人脸检测库 AipFace,由百度云 AI 开放平台提供,是一个可以进行人脸检测的 Python SDK。可以直接通过 HTTP 访问,免费使用。 5 检测过滤条件 过滤所有未出现人脸图片(比如风景图、未露脸身材照等) 过滤所有非女性(在抓取中,发现知乎男性图片基本是明星,故不考虑;存在 AipFace 性别识别不准的情况) 过滤所有非真实人物,比如动漫人物 (AipFace Human 置信度小于 0.6) 过滤所有颜值评分较低图片(AipFace beauty 属性小于 45,为了节省存储空间;再次声明,AipFace 评分无任何客观性) 6 实现逻辑 通过 Requests 发起 HTTP 请求,获取『美女』下的部分讨论列表 通过 lxml 解析抓取到的每个讨论中 HTML,获取其中所有的 img

Python3 win下pip更新为国内源

99封情书 提交于 2020-03-26 16:44:36
3 月,跳不动了?>>> 前言 Python在安装库的时候发现下载速度很慢,因为默认是使用的国外的源,我们可以更换为国内源。 国内比较好的源: 清华源: https://pypi.tuna.tsinghua.edu.cn/simple 豆瓣源: http://pypi.douban.com/simple/ 阿里源: http://mirrors.aliyun.com/pypi/simple/ 解决办法一: 在pip install 库名 -i 想要使用源 如 pip install lxml -i https://pypi.tuna.tsinghua.edu.cn/simple 解决办法二: 更换办法:在c盘C:\Users%path%\AppData\Roaming(%path%为你的用户名字)下自己创建一个pip的文件夹,在此文件下创建一个pip.ini的文件 `[global] timeout = 6000 index-url = https://mirrors.aliyun.com/pypi/simple/ trusted-host = mirrors.aliyun.com` 来源: oschina 链接: https://my.oschina.net/u/3696256/blog/3211945

【Python爬虫学习笔记4】结合Xpath与lxml库解析数据

佐手、 提交于 2020-03-24 11:07:20
在之前的学习中了解了如何使用爬虫向目标服务器发送请求并获取响应,而此后便是要对响应进行处理,这里的处理在爬虫中通常指的是数据解析,即将相应内容数据化以方便我们进行有效数据的提取。在此过程中,有许多解析数据的方法,本节介绍利用Xpath和lxml库来解析数据。 Xpath Xpath(全称XML Path Language,XML路径语言),是一门在XML和HTML文档中查找信息的语言,它提供了非常简明的路径选择表达式,可用来对网页的元素及属性进行遍历查找。 语法规则: 1.选取节点 XPath 使用路径表达式来选取 XML 文档中的节点或者节点集。 表达式 描述 示例 说明 nodename 选取此节点的所有子节点 div 选取div下所有子节点 / 从当前节点选取直接子节点 /div 从根元素下选取所有div节点 // 从当前节点选取所有子孙节点 //div 从全局节点中选取所有的div节点 @ 选取属性 //a[@class] 选取所有拥有class属性的a节点 . 选取当前节点 .//a 选取当前节点下的所有a节点 .. 选取当前节点的父节点 ..//a 选取当前节点父节点下的所有a节点 2.谓语 谓语用来查找某个特定的节点或者包含某个指定的值的节点,被嵌在方括号中。 表达式 描述 示例 说明 [index] 选取指定序列的节点 /div/p[1] 选取div下的第一个p节点

python lxml can't parse japanese in some case [duplicate]

随声附和 提交于 2020-03-23 10:44:38
问题 This question already has answers here : python requests.get() returns improperly decoded text instead of UTF-8? (4 answers) Closed 19 days ago . I am using lxml 4.5.0 to scraping data from website. it works well in the following example chrome_ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 " \ "(KHTML, like Gecko) Chrome/77.0.3864.0 Safari/537.36" with requests.Session() as s: s.headers.update({'User-Agent': chrome_ua}) resp = s.get('https://www.yahoo.co.jp') parser

python lxml can't parse japanese in some case [duplicate]

ⅰ亾dé卋堺 提交于 2020-03-23 10:44:11
问题 This question already has answers here : python requests.get() returns improperly decoded text instead of UTF-8? (4 answers) Closed 19 days ago . I am using lxml 4.5.0 to scraping data from website. it works well in the following example chrome_ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 " \ "(KHTML, like Gecko) Chrome/77.0.3864.0 Safari/537.36" with requests.Session() as s: s.headers.update({'User-Agent': chrome_ua}) resp = s.get('https://www.yahoo.co.jp') parser

lxml库

天大地大妈咪最大 提交于 2020-03-17 00:31:00
lxml库 什么是lxml库 lxml是一种使用Python编写的库,可以迅速、灵活地处理XML 支持XPath语句 学习lxml库的目的 利用所学的XPath语法,来快速定位特定元素以及节点信息 目的是提取HTML、XML目标数据 安装lxml库 pip install lxml 详情代码学习,见如下: # 导入lxml库, 调用etree方法 from lxml import etree # 准备的html数据,不完整 html,body,li标签不完整 html_data = ''' <div> <ul> <li class="item-0"><a href="link1.html">first item</a></li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-inactive"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-1"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a> </ul> </div

"error: command 'x86_64-linux-gnu-gcc' failed with

血红的双手。 提交于 2020-03-15 02:20:24
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1 原创 2014年12月30日 23:11:55 26574 在ubuntu14.04版本上安装lxml,老是出错,在一番艰辛的搜索之后 ,终于找出了安装的正确方法,其实也就是没有将依赖包装全: sudo apt-get install libxml2-dev libxslt1-dev python-dev sudo apt-get install zlib1g-dev sudo apt-get install libevent-dev sudo pip install lxml 中途执行pip install lxml可能遇到识别不正确的情况,需要apt-get update一下方可继续 参考网址: http://lxml.de/installation.html http://blog.marchtea.com/archives/104 http://blog.marchtea.com/archives/91 下面是之前的参考,也可能会安装成功 sudo apt-get install python-twisted-web python2.7-dev 安装 pip : $ wget https://bootstrap.pypa.io/get-pip.py

RobotFramework之XML

孤者浪人 提交于 2020-03-14 02:08:32
XML Library version: 3.0.4 Library scope: global Named arguments: supported Introduction Robot Framework test library for verifying and modifying XML documents. As the name implies, XML is a test library for verifying contents of XML files. In practice it is a pretty thin wrapper on top of Python's ElementTree XML API . The library has the following main usages: Parsing an XML file, or a string containing XML, into an XML element structure and finding certain elements from it for for further analysis (e.g. Parse XML and Get Element keywords). Getting text or attributes of elements (e.g. Get