python3_mechanicalsoup
python3_mechanicalsoup
# !/usr/bin/python3.4
# -*- coding: utf-8 -*-
import mechanicalsoup
# 事实证明,这个库非常的不友好
# 非常!!
# 和mechanize没法比
# https://github.com/hickford/MechanicalSoup
def openbrowser(url):
# 定义browser
browser = mechanicalsoup.Browser()
# 头部
browser.addheaders = [('User-Agent',
'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'),
('Referer', 'http://www.baidu.com'),
('Host', 'www.baidu.com')]
# 打开百度
result = browser.get(url)
# 定位搜索表格
search = result.soup.select("#form")[0]
print(search)
# 搜索框填入ttyb
search.select("#kw")[0]['value'] = "TTyb"
# 点击百度一下
page2 = browser.submit(search, result.url)
# ('UTF-8')('unicode_escape')('gbk','ignore')
html = page2.content.decode("UTF-8", "ignore")
return html
if __name__ == '__main__':
url = "http://www.baidu.com"
html = openbrowser(url)
print(html)
python3_mechanicalsoup的更多相关文章
随机推荐
- Intent的简介以及属性详解
一.Intent的介绍 Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述 ...
- iOS:测试机添加
一,首先打开开发者首页:https://developer.apple.com/,点击Member Center二,点击certificates,Identifiers & Profiles三 ...
- Fix a corrupted user profile
Fix a corrupted user profile Applies to Windows 7 Your user profile is a collection of settings that ...
- Linux 常用命令笔记
Linux 常用命令笔记 1. locate locate:用来定位文件的位置,如:locate a.txt 但是这个命令有延迟,也就是新建的文件不一定能搜索到,如果非要找到新建的文件可以使用 upd ...
- 7-zip的压缩的时候排除某目录
安装暂且不说了. 看一下帮助. [root@localhost Server]# 7z -Zip [] - Igor Pavlov -- p7zip Version ,Utf16=on,HugeFil ...
- 简单插入排序(C++版)
#include <iostream> using namespace std; /** \ Insert Sort * * Key: * * reserve: tm = a[i] * * ...
- Sprint第二个冲刺(第二天)
一.Sprint 计划会议: 在这次会议中我们主要讨论了我们正在做的几个任务,比如说在美化按钮和增添图片上我们都发表了自己的想法,卓炜杰同学也把我们的想法进行分析,寻求最适合的方法.在查看用 ...
- C++之STL之string
/*C 语言中字符数组一般会采用char str[]来存放,但是显得会比较麻烦,C++在stl中加入了string类型,对字符串常用的功能进行了封装,操作起来比较方便*/#include<cst ...
- leetcode之链表排序题
原文链接:点击打开链接 原题是这样的: Given a linked list and a value x, partition it such that all nodes less than x ...
- SlidingMenu实战系列
http://blog.csdn.net/yangyu20121224/article/category/1431917