python3_mechanicalsoup

  1. # !/usr/bin/python3.4
  2. # -*- coding: utf-8 -*-
  3. import mechanicalsoup
  4. # 事实证明,这个库非常的不友好
  5. # 非常!!
  6. # 和mechanize没法比
  7. # https://github.com/hickford/MechanicalSoup
  8. def openbrowser(url):
  9. # 定义browser
  10. browser = mechanicalsoup.Browser()
  11. # 头部
  12. browser.addheaders = [('User-Agent',
  13. 'Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5'),
  14. ('Referer', 'http://www.baidu.com'),
  15. ('Host', 'www.baidu.com')]
  16. # 打开百度
  17. result = browser.get(url)
  18. # 定位搜索表格
  19. search = result.soup.select("#form")[0]
  20. print(search)
  21. # 搜索框填入ttyb
  22. search.select("#kw")[0]['value'] = "TTyb"
  23. # 点击百度一下
  24. page2 = browser.submit(search, result.url)
  25. # ('UTF-8')('unicode_escape')('gbk','ignore')
  26. html = page2.content.decode("UTF-8", "ignore")
  27. return html
  28. if __name__ == '__main__':
  29. url = "http://www.baidu.com"
  30. html = openbrowser(url)
  31. print(html)

python3_mechanicalsoup的更多相关文章

随机推荐

  1. Intent的简介以及属性详解

    一.Intent的介绍 Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述 ...

  2. iOS:测试机添加

    一,首先打开开发者首页:https://developer.apple.com/,点击Member Center二,点击certificates,Identifiers & Profiles三 ...

  3. Fix a corrupted user profile

    Fix a corrupted user profile Applies to Windows 7 Your user profile is a collection of settings that ...

  4. Linux 常用命令笔记

    Linux 常用命令笔记 1. locate locate:用来定位文件的位置,如:locate a.txt 但是这个命令有延迟,也就是新建的文件不一定能搜索到,如果非要找到新建的文件可以使用 upd ...

  5. 7-zip的压缩的时候排除某目录

    安装暂且不说了. 看一下帮助. [root@localhost Server]# 7z -Zip [] - Igor Pavlov -- p7zip Version ,Utf16=on,HugeFil ...

  6. 简单插入排序(C++版)

    #include <iostream> using namespace std; /** \ Insert Sort * * Key: * * reserve: tm = a[i] * * ...

  7. Sprint第二个冲刺(第二天)

    一.Sprint 计划会议:      在这次会议中我们主要讨论了我们正在做的几个任务,比如说在美化按钮和增添图片上我们都发表了自己的想法,卓炜杰同学也把我们的想法进行分析,寻求最适合的方法.在查看用 ...

  8. C++之STL之string

    /*C 语言中字符数组一般会采用char str[]来存放,但是显得会比较麻烦,C++在stl中加入了string类型,对字符串常用的功能进行了封装,操作起来比较方便*/#include<cst ...

  9. leetcode之链表排序题

    原文链接:点击打开链接 原题是这样的: Given a linked list and a value x, partition it such that all nodes less than x  ...

  10. SlidingMenu实战系列

    http://blog.csdn.net/yangyu20121224/article/category/1431917