python
大西觉得海星
这个作者很懒,什么都没留下…
展开
-
把爵迹弄下来看看
import pdbimport requestsimport scheduleimport spacyimport timefrom lxml import etreedef text_info(url): ret = "" r = requests.get(url) content = r.content html = etree.HTML(content) text_info_xpath = html.xpath('//html/body/d.原创 2020-07-03 16:56:16 · 237 阅读 · 0 评论 -
全国城市python-json格式
# -*- coding: utf-8 -*-"""PROVINCE 省份CITY 城市COUNTRY 国家"""PROVINCE = [u'\u5e7f\u4e1c', u'\u5317\u4eac', u'\u4e0a\u6d77', u'\u5929\u6d25', u'\u91cd\u5e86', u'\u5b89\u5fbd', u'\u798...原创 2019-07-25 16:14:15 · 4530 阅读 · 0 评论 -
lxml一些函数说明
etree篇from lxml import etreehtml篇from lxml import htmlfromstring(content)如果源码为utf-8或者gbk编码的字符串。fromstring函数会检测网页源码中meta标签下content属性中charset,对源码进行解码。<meta http-equiv="content-type" ...原创 2019-03-07 10:30:12 · 428 阅读 · 0 评论 -
正则表达式稍微高级点的用法,不断更新。
import restr_ = "(x)abc ()def (x)ghi"print re.findall("\(x\)(.*?)(?=\(\)|$|\(x\))", str_)# >>> ['abc ', 'ghi']# 匹配所有勾选过的内容。直到下一个括号或者空括号或者行尾。 1. (pattern) 匹配pattern并获取这一匹配。 ...原创 2019-02-13 17:09:51 · 345 阅读 · 0 评论 -
python importlib
# -*- coding: utf-8 -*-from importlib import import_moduleimport pdbmodule = import_module("test2")test_class = getattr(module, "Test") # 类test_instance = test_class() # 实例化print dir(test_in...原创 2019-01-03 14:14:20 · 185 阅读 · 0 评论 -
SQL,Redis,python操作
§ redisimportimport redisfrom redis.sentinel import Sentinelredis连接方式redis_sentinel = Sentinel([(host_1, port_1), (host_2, port_2)])r = redis_sentinel.master_for("master_test", db=0, max_c...原创 2019-01-18 10:17:56 · 124 阅读 · 0 评论 -
python模块os和sys
# -*- coding: utf-8 -*-import osimport sys"""§ os"""os.name # 返回当前操作系统内核。# windows返回nt内核,linux返回posix内核。os.environ # 返回当前环境变量,全部 <type 'dict'>。os.getenv() # 获得环境变量 os.getenv("PYTHO...原创 2018-12-06 18:35:02 · 126 阅读 · 0 评论 -
pip使用国内源
阿里云 http://mirrors.aliyun.com/pypi/simple/中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/ 豆瓣(douban) http://pypi.douban.com/simple/ 清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/中国科学技术大学 http://py...原创 2018-10-20 10:17:49 · 1395 阅读 · 1 评论 -
python多线程
# -*- coding:utf-8 -*-import time# import threadimport threadingdef count_time(func): def inner(*args, **kwargs): start_time = time.time() ret = func(*args, **kwargs) ...原创 2018-09-29 09:26:39 · 144 阅读 · 0 评论 -
xpath对当前节点定位
ele = body.xpath('//*[@id="shareholderInfo"]/tbody/tr[1]/td[2]')[0]print dir(ele)ii = ele.iterancestors()list_ = []list_.append(ele.tag)print ele.tagfor i in ii: print i.tag list_.appen...原创 2018-09-07 15:55:30 · 1782 阅读 · 0 评论 -
selenium常用方法 不断更新
添加chromedriver如果chromedriver在环境变量里,不用添加chromedriver的路径。driver = webdriver.Chrome()driver = webdriver.Chrome(chromedriver_path)常用函数back() # 返回/后退close() # 关闭当前标签current_url # 当前标签的网址 f...原创 2018-09-12 11:05:21 · 459 阅读 · 0 评论 -
Phantomjs和selenium安装方法
下载 安装http://phantomjs.org/download.html解压 添加bin目录到环境变量C:\phantomjs-2.1.1-windows\bin cmd powershell里输入phantomjs -v返回正确的版本号说明安装成功 自动化测试工具selenium安装方法 pip安装最新版本 3.0.1版本seleniumpip in...原创 2018-08-31 23:41:55 · 292 阅读 · 0 评论 -
网络爬虫_re正则表达式
"""正则表达式re概念 检索符合某个规则的字符串。 通用的字符串表达框架。 简洁表达一组字符串。常用 表达文本类型的特征。 同时查找或替换一组字符串。 匹配字符串全部或部分。"""# --------------------------------------"""正则表达式语法常用操作符 . # 表示任何单字符...原创 2018-04-21 08:12:08 · 440 阅读 · 0 评论 -
网络爬虫实例
"""淘宝商品信息定向爬取"""import requestsimport redef get_html_text(url): try: r = requests.get(url, timeout=30) r.raise_for_status() r.encoding = r.apparent_encoding retu...原创 2018-04-21 09:40:19 · 433 阅读 · 0 评论 -
json简介load()loads()dump()dumps()
# -*- coding: utf-8 -*-"""JSON一种保存数据的格式。可以保存本地JSON文件,可以将JSON文件进行传输。通常将JSON成为轻量级传输方式。JSON文件组成{} # 代表对象,字典。[] # 代表列表。: # 代表键值对。, # 分隔两个部分。"""import jsonjson_str = '{"name": "tianyz", ...原创 2018-04-25 22:29:09 · 230 阅读 · 0 评论 -
requests.session()处理网页cookie
# 实例化session。# session()中方法和requests()中一样# session.get() session.post()session = requests.session()# 使用session发送post请求获取cookie保存到本地session中。# 以人人网登录为例。post_url = "http://www.renren.com/PLogin...原创 2018-05-02 22:19:43 · 2201 阅读 · 0 评论 -
lxml.xpath用法
# -*- coding: utf-8 -*-import requestsfrom lxml import etree"""url = 'http://www.baidu.com'r = requests.get(url, timeout=5)r.encoding = r.apparent_encodingprint r.status_codetext = r.text...原创 2018-07-30 14:36:04 · 888 阅读 · 0 评论 -
xpath的使用方法, 爬虫实例
# -*- coding:utf-8 -*-""" 爬虫 创业邦 创业公司信息爬取网页url = 'http://www.cyzone.cn/vcompany/list-0-0-1-0-0/0'爬取页面中的创业公司,融资阶段,创业领域,成立时间和创业公司的链接信息。使用到requests, json, codecs, lxml等库requests用于访问页面,获取页面的源代码jo...原创 2018-08-26 10:19:29 · 1800 阅读 · 1 评论