输出 'https://china-testing.github.io/' 的返回头html
In [1]: import requests In [2]: url = 'https://china-testing.github.io/' In [3]: response = requests.get(url) In [4]: response.request.headers Out[4]: {'User-Agent': 'python-requests/2.18.4', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
requests是HTTP访问极其重要的库,比较经常使用的属性有:response.status_code、response.text。html5
更多参考资料:python工具库介绍-requests:人性化的HTTPpython
爬取 https://china-testing.github.io/ 首页的博客标题,共10条.git
# -*- coding: utf-8 -*- # 讨论钉钉免费群21745728 qq群144081101 567351477 # CreateDate: 2018-10-16 import requests from bs4 import BeautifulSoup def get_upcoming_events(url): req = requests.get(url) soup = BeautifulSoup(req.text, 'lxml') events = soup.findAll('article') for event in events: event_details = {} event_details['name'] = event.find('h1').find("a").text print(event_details) get_upcoming_events('https://china-testing.github.io/')
执行结果:github
$ python3 blogs.py {'name': '接口自动化性能测试线上培训大纲'} {'name': '2018最佳人工智能图像处理工具OpenCV书籍下载'} {'name': 'IBM开发社区python精品文章汇总'} {'name': 'python工具库介绍-requests:人性化的HTTP'} {'name': '中草药的故事-金银花(标准中药)- 清热解毒,疏散风热'} {'name': '中草药的故事-合欢花(标准中药)'} {'name': '中草药的故事-吴茱萸(标准中药)'} {'name': '[雪峰磁针石博客]python3快速入门教程9重要的标准库-高级篇'} {'name': '[雪峰磁针石博客]python3快速入门教程11命令行自动化工具与pexpect'} {'name': '[雪峰磁针石博客]python3快速入门教程9重要的标准库-基础篇'}
BeautifulSoup的默认解析器为html.parser,处理大页面比较吃力,为此使用lxml。解释器html5lib的行为和浏览器表现相似。web
最新代码地址json
https://github.com/china-testing/python-api-tesing/blob/master/python-automation-cook/ch3/blogs.pyapi
用selenium访问'https://httpbin.org/forms/post',填充内容浏览器
# 讨论钉钉免费群21745728 qq群144081101 567351477 # CreateDate: 2018-10-16 from selenium import webdriver import time browser = webdriver.Chrome() browser.get('https://httpbin.org/forms/post') custname = browser.find_element_by_name("custname") custname.clear() custname.send_keys("python测试开发") time.sleep(2) for size_element in browser.find_elements_by_name("size"): if size_element.get_attribute('value') == 'medium': size_element.click() time.sleep(2) for topping in browser.find_elements_by_name('topping'): if topping.get_attribute('value') in ['bacon', 'cheese']: topping.click() time.sleep(2) browser.find_element_by_tag_name('form').submit()
执行结果app
{ "args": {}, "data": "", "files": {}, "form": { "comments": "", "custemail": "", "custname": "python\u6d4b\u8bd5\u5f00\u53d1", "custtel": "", "delivery": "", "size": "medium", "topping": [ "bacon", "cheese" ] }, "headers": { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "zh-CN,zh;q=0.9", "Cache-Control": "max-age=0", "Connection": "close", "Content-Length": "132", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "Origin": "https://httpbin.org", "Referer": "https://httpbin.org/forms/post", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36" }, "json": null, "origin": "183.62.236.90", "url": "https://httpbin.org/post" }