NoBige-JackCh3n

copy to my blog

前端CDN

Staticfile CDN/七牛 推荐,七牛CDN赞助
jsDelivr - A free, fast, and reliable Open Source CDN for npm & GitHub 推荐,建议合理使用
字节跳动静态资源公共库 推荐,阿里云CDN
UNPKG饿了么镜像
例: https://npm.elemecdn.com/jquery@3.2.1/dist/jquery.min.js
阿里云CDN
UNPKG知乎镜像
例子:http://unpkg.zhimg.com/jquery@3.2.1/dist/jquery.min.js
阿里云CDN
新浪SAE 资源较少
又拍云JS库加速服务 资源旧老
腾讯网静态资源公共库 资源旧老
阳光网常用前端公共库 资源旧老
前端通用库CDN 资源旧老
前端公共库CDN服务-淘巴士 支持部分指定版本
静态资源托管库/360 不推荐,波折过多
BootCDN - Bootstrap 中文网开源项目免费 CDN 加速服务 不推荐,每年终于那么几天
css.loli.net (原 css.net) - 常用前端公共库 CDN 服务 对大陆不友好
加速资源 - 极客族公共加速服务 对大陆不友好
前端公共库CDN加速-静态资源公共库,免费CDN公共库 对大陆不友好
cdnjs.com - The best FOSS CDN for web related libraries to speed up your websites! 对大陆不友好
UNPKG 对大陆不友好
Microsoft Ajax Content Delivery Network Microsoft Docs 对大陆不友好
jQuery UI – All Versions jQuery CDN 对大陆不友好

在 CDN 读取失败的时候从自己服务器加载

1
2
<script src="http://wlib.sinaapp.com/js/jquery/1.7.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="/js/jquery/1.7.2/jquery.min.js">\x3C/script>')</script>

Google

1
2
3
4
5
6
7
前端库
https://ajax.loli.net/
https://gapis.geekzu.org/ajax/

字体文件
https://fonts.loli.net/
https://gapis.geekzu.org/g-themes/

Gravatar

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
--------使用方式--------
b7ccb4e3ea0050a8f290e2f788eebd95 = md5(nobige@x0ox.uu.me)
http://www.gravatar.com/avatar/b7ccb4e3ea0050a8f290e2f788eebd95
http://www.gravatar.com/avatar/b7ccb4e3ea0050a8f290e2f788eebd95.png
http://www.gravatar.com/avatar/b7ccb4e3ea0050a8f290e2f788eebd95.jpg

--------运营时间长-----
官方CN
http://cn.gravatar.com/avatar/

七牛
https://dn-qiniu-avatar.qbox.me/avatar/

css.loli.net
https://gravatar.loli.net/avatar/

极客族
http://fdn.geekzu.org/avatar/
https://sdn.geekzu.org/avatar/

--------运营时间有待考验-----
Rinvay.H Blog,2018年起提供
国际
https://gravatars.o0o.fun/avatar/
国内
https://gravatar.o0o.fun/avatar/

参考

错误

You can’t specify target table ‘movies2’ for update in FROM clause

描述: 如果子查询的 from 子句和更新、删除对象使用同一张表,会出现上述错误。

解决方法: 通过给 from 子句中的结果集起别名。

如,原本要删除的子查询为

1
2
3
delete from movies2 where id in (
select id from movies2 where title like "%集%"
)

修改为

1
2
3
4
5
delete from movies2 where id in (
select nobige.id from (
select id from movies2 where title like "%集%"
) as nobige
)

同理,也可以临时创建一个查询结果表在通过此表进行删除操作。

1
2
3
4
5
6
7
-- 创建临时表
create into temp as
select id from movies2 where title like "%集%";

delete from movies2 where id in (
select * from temp
)

参考

Selenium

Selenium是一个Web的自动化测试工具,最初是为网站自动化测试而开发的,Selenium
可以直接运行在浏览器上,它支持所有主流的浏览器(包括PhantomJS这些无界面的浏览器),可以接收指令,让浏览器自动加载页面,获取需要的数据,甚至页面截屏

下载

安装

可以解决,以下错误问题

Message: ‘chromedriver’ executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

首先根据Chrome版本进行下载chromedriver驱动,

Chrome版本查看路径

[菜单 ] -> [帮助] -> [关于Google Chrome]

Chrome版本查看路径

如下图所示,我的版本为版本 77.0.3865.90(正式版本) (64 位)

Chrome版本

然后到ChromeDriver Mirror淘宝镜像地址进行下载,以我的版本为例搜索77.0.3865然后选择77.0.3865.40即可,一般都是选择对应chrome版本较新的即可。

npm淘宝镜像

(以Win7为例)接着下载对应版本的chromedriver驱动chromedriver.exe放到python.exe 同级文件夹即可,如

python path 是 C:\Users\Administrator\AppData\Local\Programs\Python\Python37

python dir

最后尝试代码以下代码,若是可以正常的打开网站即为安装成功。

1
2
3
4
5
6
7
from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://nobige.cn')
print(driver.title)

driver.quit()

错误

Message: ‘chromedriver’ executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

解决办法参考,安装。

element is not attached to the page document

原因:页面未完全加载完成(元素丢失),解决办法,刷新页面后延迟下或者重新获取

1
2
3
# 刷新
driver.refresh()
time.sleep(5)

方法

Selenium提供了下列的方法

  • find_element_by_id
  • find_element_by_name
  • 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

一次查找多个元素 (这些方法会返回一个list列表):

  • find_elements_by_name
  • find_elements_by_xpath
  • find_elements_by_link_text
  • find_elements_by_partial_link_text
  • find_elements_by_tag_name
  • find_elements_by_class_name
  • find_elements_by_css_selector

手册中找到的可能用的上的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
获取标签属性
get_attribute
get_attribute("class")

填写内容
find_element_by_id("username").send_keys("some text")

下拉或多选
清除所有选定的条目通过索引,值,文本名
deselect_all
deselect_by_index
deselect_by_value
deselect_by_visible_text

选择下拉或多选通过索引,值,文本名
select_by_index
select_by_value
select_by_visible_text

返回属于此选择标记的所有选定选项的列表
all_selected_options

此选择标记中的第一个选定选项(或正常选择中的当前选定选项)
first_selected_option

返回属于此选择标记的所有选项的列表
options

单击
click()

双击
double_click

提交
element.submit()

在不同的窗口和框架之间移动
driver.switch_to_window("windowName")

弹出对话框
alert = driver.switch_to_alert()

操作Cookies,增,查,删,删除全部
add_cookie(cookie)
get_cookies()
delete_cookie
delete_all_cookies

控制浏览器历史前进
forward()

控制浏览器历史后退
back()

刷新
refresh

关闭页面
close()

最大化 Web 驱动程序正在使用的当前窗口
maximize_window
最小化 Web 驱动程序正在使用的当前窗口
minimize_window

将当前窗口的屏幕截图保存到 PNG 图像文件中。
get_screenshot_as_file(文件名)
save_screenshot

获取当前窗口的屏幕截图作为 base64 编码字符串
get_screenshot_as_base64

获取当前URL
current_url

参考

python利用xpath定位问题

豆瓣top250

以豆瓣top250为例,xpath定位的方式是“先大后小”,即先获取到列表然后再获取单部电影的信息。

列表最一层为ol元素且有class属性,要取列表则是

1
selector.xpath('//ol[@class="grid_view"]/li')

取完后,单个信息便是简单获取了只要遍历即可,如电影封面图片,电影名字,评分。。。

1
2
3
4
5
6
7
8
# 电影封面图片
item.xpath('//img/@src')[0]

# 电影名字
item.xpath('//div[@class="hd"]/a/text()')

# 评分
item.xpath('//span[@class="rating_num"]/text()')

若到此也是按其xpath的路径来获取的话,获取的并不是列表里面单个电影的信息,而是网页中全部符合路径的信息,为什么?原因和简单,其主要问题在于路径定位问题。

若是改成绝对定位也是可以,如下

1
2
# 电影封面图片
item.xpath('//*[@id="content"]/div/div[/ol/li/div/div[1]/a/img/@src')[0]

但是获取列表就没意义,解决办法只需要一个 . 修改之后的定位则是

1
2
3
4
5
6
7
8
# 电影封面图片
item.xpath('.//img/@src')[0]

# 电影名字
item.xpath('.//div[@class="hd"]/a/text()')

# 评分
item.xpath('.//span[@class="rating_num"]/text()')

可以理解为,当前路径下开始寻找。

XPath 语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 获取title
item.xpath('//title/text()')
# 获取图片链接
item.xpath('.//img/@src')
# 获取图片描述
item.xpath('.//img/@alt')
# 获取图片data-src
item.xpath('.//img/@data-src')
# 其他同理

# 获取指定节点下的所有节点,包含注释内容
item.xpath('//div[@class="jjText"]/node()')
# 再进一步进行提取注释内容

# 获取全部注释内容
item.xpath('.//comment()')

/ 根节点选取、或是元素和元素的过度。
//

xpath获取某个节点的html源码

1
2
3
4
5
6
7
8
9
10
11
import requests
import lxml.html
import html
temp_html = requests.get('https://www.thepaper.cn/newsDetail_forward_8054822').content.decode('utf-8')
selector = lxml.html.fromstring(temp_html)
temp_txt = selector.xpath('//div[@class="news_txt"]')
temp_htmls = ''
for x in temp_txt:
temp_htmls += html.unescape(lxml.html.tostring(x).decode('utf-8'))
# html.unescape 是html转码,解析,转义
print(temp_htmls)

选取节点

XPath 使用路径表达式在 XML 文档中选取节点。节点是通过沿着路径或者 step 来选取的。

下面列出了最有用的路径表达式:

表达式 描述
nodename 选取此节点的所有子节点。
/ 从根节点选取。
// 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置。
. 选取当前节点。
.. 选取当前节点的父节点。
@ 选取属性。

实例

在下面的表格中,我们已列出了一些路径表达式以及表达式的结果:

路径表达式 结果
bookstore 选取 bookstore 元素的所有子节点。
/bookstore 选取根元素 bookstore。注释:假如路径起始于正斜杠( / ),则此路径始终代表到某元素的绝对路径!
bookstore/book 选取属于 bookstore 的子元素的所有 book 元素。
//book 选取所有 book 子元素,而不管它们在文档中的位置。
bookstore//book 选择属于 bookstore 元素的后代的所有 book 元素,而不管它们位于 bookstore 之下的什么位置。
//@lang 选取名为 lang 的所有属性。

谓语(Predicates)

谓语用来查找某个特定的节点或者包含某个指定的值的节点。

谓语被嵌在方括号中。

实例

在下面的表格中,我们列出了带有谓语的一些路径表达式,以及表达式的结果:

路径表达式 结果
/bookstore/book[1] 选取属于 bookstore 子元素的第一个 book 元素。
/bookstore/book[last()] 选取属于 bookstore 子元素的最后一个 book 元素。
/bookstore/book[last()-1] 选取属于 bookstore 子元素的倒数第二个 book 元素。
/bookstore/book[position()<3] 选取最前面的两个属于 bookstore 元素的子元素的 book 元素。
//title[@lang] 选取所有拥有名为 lang 的属性的 title 元素。
//title[@lang=’eng’] 选取所有 title 元素,且这些元素拥有值为 eng 的 lang 属性。
/bookstore/book[price>35.00] 选取 bookstore 元素的所有 book 元素,且其中的 price 元素的值须大于 35.00。
/bookstore/book[price>35.00]/title 选取 bookstore 元素中的 book 元素的所有 title 元素,且其中的 price 元素的值须大于 35.00。

选取未知节点

XPath 通配符可用来选取未知的 XML 元素。

通配符 描述
* 匹配任何元素节点。
@* 匹配任何属性节点。
node() 匹配任何类型的节点。

实例

在下面的表格中,我们列出了一些路径表达式,以及这些表达式的结果:

路径表达式 结果
/bookstore/* 选取 bookstore 元素的所有子元素。
//* 选取文档中的所有元素。
//title[@*] 选取所有带有属性的 title 元素。

选取若干路径

通过在路径表达式中使用“|”运算符,您可以选取若干个路径。

实例

在下面的表格中,我们列出了一些路径表达式,以及这些表达式的结果:

路径表达式 结果
//book/title | //book/price 选取 book 元素的所有 title 和 price 元素。
//title | //price 选取文档中的所有 title 和 price 元素。
/bookstore/book/title | //price 选取属于 bookstore 元素的 book 元素的所有 title 元素,以及文档中所有的 price 元素。

参考

net::ERR_BLOCKED_BY_CLIENT

chrome Console 提示net::ERR_BLOCKED_BY_CLIENT
原因
广告过滤插件拦截,可能是误判也许是被链接里面包含ad这两个目录
解决
将测试网站加入白名单或者禁用过滤广告插件

ERR_BLOCKED_BY_CLIENT

0%