本文整理汇总了Python中tests.util.build_grab函数的典型用法代码示例。如果您正苦于以下问题:Python build_grab函数的具体用法?Python build_grab怎么用?Python build_grab使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了build_grab函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_session
def test_session(self):
# Test that if Grab gets some cookies from the server
# then it sends it back
grab = build_grab()
grab.setup(reuse_cookies=True)
self.server.response['cookies'] = {'foo': 'bar'}.items()
grab.go(self.server.get_url())
self.assertEqual(grab.doc.cookies['foo'], 'bar')
grab.go(self.server.get_url())
self.assertEqual(self.server.request['headers']['Cookie'], 'foo=bar')
grab.go(self.server.get_url())
self.assertEqual(self.server.request['headers']['Cookie'], 'foo=bar')
# Test reuse_cookies=False
grab = build_grab()
grab.setup(reuse_cookies=False)
self.server.response['cookies'] = {'foo': 'baz'}.items()
grab.go(self.server.get_url())
self.assertEqual(grab.doc.cookies['foo'], 'baz')
grab.go(self.server.get_url())
self.assertTrue(len(self.server.request['cookies']) == 0)
# Test something
grab = build_grab()
grab.setup(reuse_cookies=True)
self.server.response['cookies'] = {'foo': 'bar'}.items()
grab.go(self.server.get_url())
self.assertEqual(grab.doc.cookies['foo'], 'bar')
grab.clear_cookies()
grab.go(self.server.get_url())
self.assertTrue(len(self.server.request['cookies']) == 0)
开发者ID:lorien,项目名称:grab,代码行数:31,代码来源:grab_cookies.py
示例2: test_unicode_post
def test_unicode_post(self):
# By default, unicode post should be converted into utf-8
grab = build_grab()
data = u'фыва'
grab.setup(post=data, url=self.server.get_url())
grab.request()
self.assertEqual(self.server.request['data'], data.encode('utf-8'))
# Now try cp1251 with charset option
self.server.request['charset'] = 'cp1251'
grab = build_grab()
data = u'фыва'
grab.setup(post=data, url=self.server.get_url(),
charset='cp1251', debug=True)
grab.request()
self.assertEqual(self.server.request['data'], data.encode('cp1251'))
# Now try dict with unicode value & charset option
self.server.request['charset'] = 'cp1251'
grab = build_grab()
data = u'фыва'
grab.setup(post={'foo': data}, url=self.server.get_url(),
charset='cp1251', debug=True)
grab.request()
test = 'foo=%s' % quote(data.encode('cp1251'))
test = test.encode('utf-8') # py3 hack
self.assertEqual(self.server.request['data'], test)
开发者ID:lorien,项目名称:grab,代码行数:27,代码来源:grab_post_request.py
示例3: test_dash_issue
def test_dash_issue(self):
html = '<strong>—</strong>'
self.server.response['get.data'] = html
grab = build_grab()
grab.go(self.server.get_url())
# By default &#[128-160]; are fixed
self.assertFalse(grab.doc.select('//strong/text()').text()
== six.unichr(151))
self.assertTrue(grab.doc.select('//strong/text()').text()
== six.unichr(8212))
# disable fix-behaviour
grab.setup(fix_special_entities=False)
grab.go(self.server.get_url())
# By default &#[128-160]; are fixed
self.assertTrue(grab.doc.select('//strong/text()').text()
== six.unichr(151))
self.assertFalse(grab.doc.select('//strong/text()').text()
== six.unichr(8212))
# Explicitly use unicode_body func
grab = build_grab()
grab.go(self.server.get_url())
#print(':::', grab.doc.unicode_body())
self.assertTrue('—' in grab.doc.unicode_body())
开发者ID:lorien,项目名称:grab,代码行数:27,代码来源:grab_charset.py
示例4: test_load_dump
def test_load_dump(self):
with temp_file() as tmp_file:
grab = build_grab()
cookies = {'foo': 'bar', 'spam': 'ham'}
grab.setup(cookies=cookies)
grab.go(self.server.get_url())
grab.cookies.save_to_file(tmp_file)
with open(tmp_file) as inp:
self.assertEqual(set(cookies.items()),
set((x['name'], x['value'])
for x in json.load(inp)))
grab = build_grab()
cookies = {'foo': 'bar', 'spam': u'begemot'}
grab.setup(cookies=cookies)
grab.go(self.server.get_url())
grab.cookies.save_to_file(tmp_file)
with open(tmp_file) as inp:
self.assertEqual(set(cookies.items()),
set((x['name'], x['value'])
for x in json.load(inp)))
# Test load cookies
grab = build_grab()
cookies = [{'name': 'foo', 'value': 'bar',
'domain': self.server.address},
{'name': 'spam', 'value': u'begemot',
'domain': self.server.address}]
with open(tmp_file, 'w') as out:
json.dump(cookies, out)
grab.cookies.load_from_file(tmp_file)
self.assertEqual(set(grab.cookies.items()),
set((x['name'], x['value']) for x in cookies))
开发者ID:lorien,项目名称:grab,代码行数:33,代码来源:grab_cookies.py
示例5: test_make_url_absolute
def test_make_url_absolute(self):
grab = build_grab()
self.server.response['get.data'] = '<base href="http://foo/bar/">'
grab.go(self.server.get_url())
absolute_url = grab.make_url_absolute('/foobar', resolve_base=True)
self.assertEqual(absolute_url, 'http://foo/foobar')
grab = build_grab()
absolute_url = grab.make_url_absolute('/foobar')
self.assertEqual(absolute_url, '/foobar')
开发者ID:lorien,项目名称:grab,代码行数:9,代码来源:grab_api.py
示例6: test_adopt
def test_adopt(self):
grab = build_grab()
self.server.response['get.data'] = 'Moon'
grab.go(self.server.get_url())
grab2 = build_grab()
self.assertEqual(grab2.config['url'], None)
grab2.adopt(grab)
self.assertTrue(b'Moon' in grab2.doc.body)
self.assertEqual(grab2.config['url'], self.server.get_url())
开发者ID:lorien,项目名称:grab,代码行数:9,代码来源:grab_api.py
示例7: test_empty_document
def test_empty_document(self):
self.server.response['get.data'] = 'oops'
grab = build_grab()
grab.go(self.server.get_url())
grab.xpath_exists('//anytag')
self.server.response['get.data'] = '<frameset></frameset>'
grab = build_grab()
grab.go(self.server.get_url())
grab.xpath_exists('//anytag')
开发者ID:lorien,项目名称:grab,代码行数:10,代码来源:ext_lxml.py
示例8: test_useragent
def test_useragent(self):
grab = build_grab()
# Null value activates default random user-agent
# For some transports it just allow them to send default user-agent
# like in Kit transport case
grab = build_grab()
grab.setup(user_agent=None)
grab.go(self.server.get_url())
self.assertTrue(len(self.server.request['headers']) > 0)
self.assertFalse('PycURL' in
self.server.request['headers']['user-agent'])
# By default user_agent is None => random user agent is generated
grab = build_grab()
grab.go(self.server.get_url())
self.assertTrue(len(self.server.request['headers']) > 0)
self.assertFalse('PycURL' in
self.server.request['headers']['user-agent'])
# Simple case: setup user agent manually
grab.setup(user_agent='foo')
grab.go(self.server.get_url())
self.assertEqual(self.server.request['headers']['user-agent'], 'foo')
with temp_file() as ua_file:
# user agent from file should be loaded
with open(ua_file, 'w') as out:
out.write('GOD')
grab.setup(user_agent=None, user_agent_file=ua_file)
grab.go(self.server.get_url())
self.assertEqual(self.server.request['headers']['user-agent'],
'GOD')
with temp_file() as ua_file:
# random user agent from file should be loaded
with open(ua_file, 'w') as out:
out.write('GOD1\nGOD2')
grab.setup(user_agent=None, user_agent_file=ua_file)
grab.go(self.server.get_url())
self.assertTrue(self.server.request['headers']['user-agent']
in ('GOD1', 'GOD2'))
agent = grab.config['user_agent']
# User-agent should not change
grab.go(self.server.get_url())
self.assertEqual(self.server.request['headers']['user-agent'], agent)
# User-agent should not change
grab.go(self.server.get_url())
self.assertEqual(self.server.request['headers']['user-agent'], agent)
开发者ID:lorien,项目名称:grab,代码行数:51,代码来源:grab_user_agent.py
示例9: test_cdata_issue
def test_cdata_issue(self):
self.server.response['data'] = XML
# By default HTML DOM builder is used
# It handles CDATA incorrectly
grab = build_grab()
grab.go(self.server.get_url())
self.assertEqual(None, grab.xpath_one('//weight').text)
self.assertEqual(None, grab.doc.tree.xpath('//weight')[0].text)
# But XML DOM builder produces valid result
# self.assertEqual(None, grab.xpath_one('//weight').text)
grab = build_grab(content_type='xml')
grab.go(self.server.get_url())
self.assertEqual('30', grab.doc.tree.xpath('//weight')[0].text)
开发者ID:lorien,项目名称:grab,代码行数:15,代码来源:ext_lxml.py
示例10: test_useragent_simple
def test_useragent_simple(self):
grab = build_grab()
# Simple case: setup user agent manually
grab.setup(user_agent='foo')
grab.go(self.server.get_url())
self.assertEqual(self.server.request['headers']['user-agent'], 'foo')
开发者ID:lorien,项目名称:grab,代码行数:7,代码来源:grab_user_agent.py
示例11: test_grab_parse_defensedxml
def test_grab_parse_defensedxml(self):
with temp_dir() as tmp_dir:
injection_path = os.path.join(tmp_dir, 'injection')
with open(injection_path, 'w') as out:
out.write('Hey there!')
# Prepare file:// URL valid for both linux and windows
injection_url = 'file:///%s' % (injection_path.lstrip('/')
.replace('\\', '/'))
bad_xml = (
'<!DOCTYPE external ['
'<!ENTITY ee SYSTEM "' + injection_url + '">'
']>'
'<root>ⅇ</root>'
).encode()
xml_file = os.path.join(tmp_dir, 'bad.xml')
# On windows, use slashed instead of backslashes to avoid error:
# Invalid file://hostname/, expected localhost or 127.0.0.1 or none
if '\\' in xml_file:
xml_file = xml_file.replace('\\', '/')
with open(xml_file, 'wb') as out:
out.write(bad_xml)
grab = build_grab(content_type='xml')
file_url = 'file://%s' % xml_file
grab.go(file_url)
self.assertRaises(EntitiesForbidden, grab.doc, '//title')
开发者ID:lorien,项目名称:grab,代码行数:25,代码来源:grab_defusedxml.py
示例12: test_invalid_charset
def test_invalid_charset(self):
html = '''<head><meta http-equiv="Content-Type"
content="text/html; charset=windows-874">'
</head><body>test</body>'''
self.server.response['get.data'] = html
grab = build_grab()
grab.go(self.server.get_url())
开发者ID:lorien,项目名称:grab,代码行数:7,代码来源:grab_charset.py
示例13: test_assign_unicode_to_body
def test_assign_unicode_to_body(self):
grab = build_grab()
grab.doc.body = b'abc'
grab.doc.body = b'def'
with self.assertRaises(GrabMisuseError):
grab.doc.body = u'Спутник'
开发者ID:lorien,项目名称:grab,代码行数:7,代码来源:grab_response_body_processing.py
示例14: test_set_methods
def test_set_methods(self):
grab = build_grab()
self.server.response['get.data'] = FORMS_HTML
grab.go(self.server.get_url())
# pylint: disable=protected-access
self.assertEqual(grab.doc._lxml_form, None)
# pylint: enable=protected-access
grab.doc.set_input('gender', '1')
# pylint: disable=no-member,protected-access
self.assertEqual('common_form', grab.doc._lxml_form.get('id'))
# pylint: enable=no-member,protected-access
# pylint: disable=no-member,protected-access
self.assertRaises(KeyError,
lambda: grab.doc.set_input('query', 'asdf'))
# pylint: enable=no-member,protected-access
grab.doc._lxml_form = None # pylint: disable=protected-access
grab.doc.set_input_by_id('search_box', 'asdf')
# pylint: disable=no-member,protected-access
self.assertEqual('search_form', grab.doc._lxml_form.get('id'))
# pylint: enable=no-member,protected-access
grab.doc.choose_form(xpath='//form[@id="common_form"]')
grab.doc.set_input_by_number(0, 'asdf')
# pylint: disable=no-member,protected-access
grab.doc._lxml_form = None
grab.doc.set_input_by_xpath('//*[@name="gender"]', '2')
self.assertEqual('common_form', grab.doc._lxml_form.get('id'))
开发者ID:lorien,项目名称:grab,代码行数:32,代码来源:ext_form.py
示例15: test_submit
def test_submit(self):
grab = build_grab()
self.server.response['get.data'] = POST_FORM % self.server.get_url()
grab.go(self.server.get_url())
grab.doc.set_input('name', 'Alex')
grab.submit()
self.assert_equal_qs(self.server.request['data'],
b'name=Alex&secret=123')
# Default submit control
self.server.response['get.data'] = MULTIPLE_SUBMIT_FORM
grab.go(self.server.get_url())
grab.submit()
self.assert_equal_qs(self.server.request['data'],
b'secret=123&submit1=submit1')
# Selected submit control
self.server.response['get.data'] = MULTIPLE_SUBMIT_FORM
grab.go(self.server.get_url())
grab.submit(submit_name='submit2')
self.assert_equal_qs(self.server.request['data'],
b'secret=123&submit2=submit2')
# Default submit control if submit control name is invalid
self.server.response['get.data'] = MULTIPLE_SUBMIT_FORM
grab.go(self.server.get_url())
grab.submit(submit_name='submit3')
self.assert_equal_qs(self.server.request['data'],
b'secret=123&submit1=submit1')
开发者ID:lorien,项目名称:grab,代码行数:29,代码来源:ext_form.py
示例16: test_nobody
def test_nobody(self):
grab = build_grab()
grab.setup(nobody=True)
self.server.response['get.data'] = 'foo'
grab.go(self.server.get_url())
self.assertEqual(b'', grab.doc.body)
self.assertTrue(len(grab.doc.head) > 0)
开发者ID:lorien,项目名称:grab,代码行数:7,代码来源:grab_limit_option.py
示例17: test_xml_with_declaration
def test_xml_with_declaration(self):
self.server.response['get.data'] =\
b'<?xml version="1.0" encoding="UTF-8"?>'\
b'<root><foo>foo</foo></root>'
grab = build_grab()
grab.go(self.server.get_url())
self.assertTrue(grab.doc.select('//foo').text() == 'foo')
开发者ID:lorien,项目名称:grab,代码行数:7,代码来源:grab_xml_processing.py
示例18: test_request_counter
def test_request_counter(self):
import threading
reset_request_counter()
grab = build_grab()
grab.go(self.server.get_url())
self.assertEqual(grab.request_counter, 1)
grab.go(self.server.get_url())
self.assertEqual(grab.request_counter, 2)
def func():
grab = build_grab()
grab.go(self.server.get_url())
# Make 10 requests in concurrent threads
threads = []
for _ in six.moves.range(10):
thread = threading.Thread(target=func)
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
grab.go(self.server.get_url())
self.assertEqual(grab.request_counter, 13)
开发者ID:lorien,项目名称:grab,代码行数:26,代码来源:grab_api.py
示例19: test_body_maxsize
def test_body_maxsize(self):
grab = build_grab()
grab.setup(body_maxsize=100)
self.server.response['get.data'] = 'x' * 1024 * 1024
grab.go(self.server.get_url())
# Should be less 50kb
self.assertTrue(len(grab.doc.body) < 50000)
开发者ID:lorien,项目名称:grab,代码行数:7,代码来源:grab_limit_option.py
示例20: test_cookiefile
def test_cookiefile(self):
with temp_file() as tmp_file:
grab = build_grab()
cookies = [{'name': 'spam', 'value': 'ham',
'domain': self.server.address}]
with open(tmp_file, 'w') as out:
json.dump(cookies, out)
# One cookie are sent in server reponse
# Another cookies is passed via the `cookiefile` option
self.server.response['cookies'] = {'godzilla': 'monkey'}.items()
grab.setup(cookiefile=tmp_file, debug=True)
grab.go(self.server.get_url())
self.assertEqual(self.server.request['cookies']['spam']['value'],
'ham')
# This is correct reslt of combining two cookies
merged_cookies = [('godzilla', 'monkey'), ('spam', 'ham')]
# grab.cookies should contains merged cookies
self.assertEqual(set(merged_cookies),
set(grab.cookies.items()))
# `cookiefile` file should contains merged cookies
with open(tmp_file) as inp:
self.assertEqual(set(merged_cookies),
set((x['name'], x['value'])
for x in json.load(inp)))
# Just ensure it works
grab.go(self.server.get_url())
开发者ID:lorien,项目名称:grab,代码行数:32,代码来源:grab_cookies.py
注:本文中的tests.util.build_grab函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论