lxml

Using an XML catalog with Python's lxml?

一曲冷凌霜 提交于 2021-01-22 06:02:52
问题 Is there a way, when I parse an XML document using lxml, to validate that document against its DTD using an external catalog file? I need to be able to work the fixed attributes defined in a document’s DTD. 回答1: You can add the catalog to the XML_CATALOG_FILES environment variable: os.environ['XML_CATALOG_FILES'] = 'file:///to/my/catalog.xml' See this thread. Note that entries in XML_CATALOG_FILES are space-separated URLs. You can use Python's pathname2url and urljoin (with file: ) to

Using an XML catalog with Python's lxml?

拟墨画扇 提交于 2021-01-22 06:01:48
问题 Is there a way, when I parse an XML document using lxml, to validate that document against its DTD using an external catalog file? I need to be able to work the fixed attributes defined in a document’s DTD. 回答1: You can add the catalog to the XML_CATALOG_FILES environment variable: os.environ['XML_CATALOG_FILES'] = 'file:///to/my/catalog.xml' See this thread. Note that entries in XML_CATALOG_FILES are space-separated URLs. You can use Python's pathname2url and urljoin (with file: ) to

python实现爬取指定bilibili视频的弹幕并制作词云

匆匆过客 提交于 2021-01-12 20:01:30
先看下最终实现的效果 具体实现思路是 1.爬取带有弹幕信息的网页 2.处理爬取得到的内容并提取所需要的弹幕信息,然后写入文本中 3.通过词云库将文本处理成想要的图片 所需要用到的库 import requests from bs4 import BeautifulSoup import pandas as pd import re import jieba from wordcloud import WordCloud from scipy.misc import imread import matplotlib.pyplot as plt 首先爬取想要的信息 ps(哔哩哔哩的弹幕全部保存在 http://comment.bilibili.com/ 122512779 .xml 中,红色字体为该视频的cid,可以在当前视频页通过:查看网页源代码—ctrl+f查找cid-出现的第一个9位cid,来获取) url = ' http://comment.bilibili.com/.xml ' # 对方的url header = { ' User-Agent ' : ' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari

这个开源的“抢茅台脚本”,火了

谁说胖子不能爱 提交于 2021-01-06 15:35:29
作者 | 写代码的明哥 来源 | Python编程时光 12 月我在朋友圈看到非常多的人开始在某东上抢茅台,抢到的话一瓶只要 1499 元,转手一卖就能净赚 1000 块钱,这简直就是白送钱的事嘛?就算不卖,自己囤着过个几年价格肯定又要上涨。像这种好事,我当然第一时间去体验了,抢了几天后,却抢了个寂寞。 好在前两天看到有大佬在 Github 上开源了一个抢茅台的脚本,目前已经 1.4k ,我前一天看的时候才 500 star,是真的火起来了,不得不感叹,这传播速度也太大了吧。 看了下代码,发现这个脚本还是用 Python 写的,我啪的一下,很快,就把运行环境给搭建起来了。 环境搭建 第一步 把项目代码下载到本地并解压。 第二步 进入到解压后的目录中,使用 venv 创建一个虚拟环境,注意一定要使用 Python 3,创建完成后进入虚拟环境 # 创建虚拟环境$ python3 -m venv .# 进入虚拟环境$ source bin/activate 第三步: 往虚拟环境中安装依赖包 如果是 mac 或者 linux 只要执行这条命令就行 $ python -m pip install -r requirements.txt 而如果你使用 windows ,在依赖中有一个 lxml 库,这个库在 windows 中你使用 pip 是安装不上的,你得从网上下载 wheel

python lxml 库

邮差的信 提交于 2021-01-01 07:37:47
html = etree.HTML(str/bytes) 参数可以是str或bytes类型,返回值是etree._Element。 调用etree.parse('hello.html'),参数是文件路径,返回值是etree._ElementTree。 etree.tostring(html,encoding='unicode') 不加编码,返回bytes,加了返回str。 etree.parse()读取文件之后用xpath不成功。<html xmlns="http://www.w3.org/1999/xhtml">把xmlns属性去掉就可以。 但是用文件以二进制打开,etree.HTML再用xpath就可以。 …… 用文本文件打开,再用etree.HTML就不行。 Traceback (most recent call last): File " d:\我的文档\py\test\tieba\qu.py " , line 53, in <module> html = etree.HTML(html2) File " src\lxml\etree.pyx " , line 3178, in lxml.etree.HTML (src\lxml\etree.c:80497 ) File " src\lxml\parser.pxi " , line 1866, in lxml.etree.

在使用python的lxml库的时候要注意的依赖问题

巧了我就是萌 提交于 2020-12-31 20:14:38
在使用python的lxml库的时候要注意的依赖问题 用pip安装lxml的时候,经常会遇到报错。 通常这跟依赖库有关系,比如libxslt-dev这个包没装。 强烈建议开发人员把生产服务器和开发机配置做100%同步,这样可以避免很多问题。 想交流这个库的同好可以联络我的微博: @王文文IT 来源: oschina 链接: https://my.oschina.net/u/4398470/blog/4872228

用PYTHON爬虫简单爬取网络小说

余生长醉 提交于 2020-12-31 10:11:52
用PYTHON爬虫简单爬取网络小说。 这里是17K小说网上,随便找了一本小说,名字是《千万大奖》。 里面主要是三个函数: 1、get_download_url() 用于获取该小说的所有章节的URL。 分析了该小说的目录页http://www.17k.com/list/2819620.html的HTML源码,发现其目录是包含在Volume里的A标签合集。所以就提取出了URLS列表。 2、get_contents(target) 用于获取小说指定章节的正文内容 分析了小说中第一章节的页面http://www.17k.com/chapter/2819620/34988369.html,发现其正文内容包含在P标签中,正文标题包含在H1标签中,经过对换行等处理,得到正文内容。传入参数是上一函数得到的URL。 3、writer(name, path, text) 用于将得到的正文内容和章节标题写入到千万大奖.txt 理论上,该简单爬虫可以爬取该网站的任意小说。 from bs4 import BeautifulSoup import requests, sys target='http://www.17k.com/list/2819620.html' server='http://www.17k.com' urls=[] def get_download_url(): req =

火了,Python「抢茅台脚本」开源

余生长醉 提交于 2020-12-31 04:57:20
12 月我在朋友圈看到非常多的人开始在某东上抢茅台,抢到的话一瓶只要 1499 元,转 手一卖就能净赚 1000 块钱,这简直就是白送钱的事嘛? 就算不卖,自己囤着过个几年价格肯定又要上涨。 像这种好事,我当然第一时间去体验了,抢了几天后,却抢了个寂寞。 好在前两天看到有大佬在 Github 上开源了一个抢茅台的脚本(https://github.com/huanghyw/jd_seckill),目前已经 1.4k ,我前一天看的时候才 500 star,是真的火起来了,不得不感叹,这传播速度也太大了吧。 看了下代码,发现这个脚本还是用 Python 写的,我啪的一下,很快,就把运行环境给搭建起来了。 1. 环境搭建 第一步 把项目代码(https://github.com/huanghyw/jd_seckill)下载到本地并解压。 第二步 进入到解压后的目录中,使用 venv 创建一个虚拟环境,注意一定要使用 Python 3,创建完成后进入虚拟环境 # 创建虚拟环境 $ python3 -m venv . # 进入虚拟环境 $ source bin/activate 第三步 : 往虚拟环境中安装依赖包 如果是 mac 或者 linux 只要执行这条命令就行 $ python -m pip install -r requirements.txt 而如果你使用 windows

Can't get text values using XPATH in python

谁说胖子不能爱 提交于 2020-12-13 11:44:28
问题 I'm trying to parse currencies from this bank website. In code: import requests import time import logging from retrying import retry from lxml import html logging.basicConfig(filename='info.log', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') @retry(wait_fixed=5000) def fetch_data_from_nb_ved_ru(): try: page = requests.get('http://www.nbu.com/exchange_rates') #print page.text tree = (html.fromstring(page.text)) #fetched_ved_usd_buy = tree.xpath('//div[@class="exchangeRates"]

python lxml loop through all tags

假装没事ソ 提交于 2020-12-13 09:34:59
问题 I have a dict mapping each xml tag to a dict key. I want to loop through each tag and text field in the xml, and compare it with the associated dict key value which is the key in another dict. <2gMessage> <Request> <pid>daemon</pid> <emf>123456</emf> <SENum>2041788209</SENum> <MM> <MID>jbr1</MID> <URL>http://jimsjumbojoint.com</URL> </MM> <AppID>reddit</AppID> <CCS> <Mode> <SomeDate>true</CardPresent> <Recurring>false</Recurring> </Mode> <Date> <ASCII>B4788250000028291^RRR