美文网首页
Python网络数据采集6-隐含输入字段

Python网络数据采集6-隐含输入字段

作者: sunhaiyu | 来源:发表于2017-07-19 15:36 被阅读49次

Python网络数据采集6-隐含输入字段

selenium的get_cookies可以轻松获取所有cookie。

from pprint import pprint
from selenium import webdriver


driver = webdriver.PhantomJS(executable_path=r'C:\Program Files (x86)\phantomjs\bin\phantomjs.exe')
driver.get('https://pythonscraping.com')
# 可以隐式等待几秒
driver.implicitly_wait(1)
pprint(driver.get_cookies())
driver.close()
[{'domain': '.pythonscraping.com',
  'expires': '周三, 19 7月 2017 01:54:21 GMT',
  'expiry': 1500429261,
  'httponly': False,
  'name': '_gat',
  'path': '/',
  'secure': False,
  'value': '1'},
 {'domain': '.pythonscraping.com',
  'expires': '周四, 20 7月 2017 01:53:21 GMT',
  'expiry': 1500515601,
  'httponly': False,
  'name': '_gid',
  'path': '/',
  'secure': False,
  'value': 'GA1.2.1017155976.1500429202'},
 {'domain': '.pythonscraping.com',
  'expires': '周五, 19 7月 2019 01:53:21 GMT',
  'expiry': 1563501201,
  'httponly': False,
  'name': '_ga',
  'path': '/',
  'secure': False,
  'value': 'GA1.2.548627101.1500429202'},
 {'domain': 'pythonscraping.com',
  'httponly': False,
  'name': 'has_js',
  'path': '/',
  'secure': False,
  'value': '1'}]

除此之外,还可以使用

  • get_cookie(name) 获取Cookie字典中键为name的值
  • add_cookie(cookie_dict) 添加Cookie
  • delete_cookie(name) 删除Cookie中某个键
  • delete_all_cookies() 删除所有Cookie

陷阱--隐含输入字段

在HTML表单中,“隐含”字段可以让字段的值对浏览器可见,但是对用户不可见(除非看网页源代码)。隐含字段主要用于阻止爬虫自动提交表单。用隐含字段组织网络采集的方式主要有两种。

第一种是表单页面上的某个字段可以用服务器生成的随机变量表示。如果提交时候这个值没有填写或者填写错误(与服务端存储的“答案”不一致),那么服务器就会拒绝我们的请求。

第二种方式是有些隐藏起来的普通字段,比如usernameemail写爬虫的可能看到这些字段就像填写,这是一个圈套。服务器会将所有填入的隐含字段的值(或者与表单提交页面默认值不同的值)忽略,而且填写了这些隐含字段的用户可能被网站封杀。比如这个网站网页源码如下。

<html><head>
    <title>A bot-proof form</title>
<style>
body {
    overflow-x:hidden;
}
.customHidden {
    position:absolute;
    right:50000px;
}
</style><style></style></head>

<body>
    <h2>A bot-proof form</h2>
<a style="display:none;" href="http://pythonscraping.com/dontgohere">Go here!</a>
<a href="http://pythonscraping.com">Click me!</a>
<form>
<input name="phone" type="hidden" value="valueShouldNotBeModified"><p>
<input name="email" class="customHidden" type="text" value="intentionallyBlank"></p><p>
<input name="firstName" type="text"></p><p>
<input name="lastName" type="text"></p><p>
<input type="submit" value="Submit"></p><p>
</p></form>

</body></html>

看到有一段<style>了吗?这个页面包含了两个链接,一个通过CSS隐藏了,另外一个可见(Click me!)另外页面还有两个隐含字段。他们分别是:

  • 第一个链接通过CSS属性设置 style="display:none"进行隐藏
  • 电话号码name="phone"指定了type="hidden"
  • 邮箱地址name="email"指定了一个自定义的隐藏,它将元素向右移动50000像素,应该都超出电脑显示器的边界了,而且隐藏了滚动条。

使用selenium的is_displayed()可以区分页面上的可见元素和不可见元素。

from selenium import webdriver

driver = webdriver.PhantomJS(executable_path=r'C:\Program Files (x86)\phantomjs\bin\phantomjs.exe')
driver.get('http://pythonscraping.com/pages/itsatrap.html')
links = driver.find_elements_by_tag_name('a')

for link in links:
    if not link.is_displayed():
        print(link.get_attribute('href'), 'is a trap!')

fields = driver.find_elements_by_tag_name('input')
for field in fields:
    if not field.is_displayed():
        print("Don't change the value of", field.get_attribute('name'))

http://pythonscraping.com/dontgohere is a trap!
Don't change the value of phone
Don't change the value of email

by @sunhaiyu

2017.7.19

相关文章

网友评论

      本文标题:Python网络数据采集6-隐含输入字段

      本文链接:https://www.haomeiwen.com/subject/fzvlkxtx.html