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的更多相关文章
随机推荐
- 【Zhejiang University PATest】02-1. Reversing Linked List
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...
- Magento后台表单字段添加备注
Magento的后台表单封装的非常好,各种字段都能够直接找到方法调用.在最近的一个项目中,为客户定制了一款定时变价功能,该功能需要导入一个csv作为变价的基础.为了方便客户,我们需要在上传表单位置添加 ...
- Qt的QLineEdit显示密码
ui->lineEdit->setEchoMode( QLineEdit::Password );
- 【题解】【位操作】【Leetcode】Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
- Word Ladder II
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- 【转】ROC和AUC介绍以及如何计算AUC
转自:https://www.douban.com/note/284051363/ ROC(Receiver Operating Characteristic)曲线和AUC常被用来评价一个二值分类器( ...
- windows服务与桌面交互
最近做服务与桌面交互的尝试,结果发现windows service 无法和桌面程序进行交互,后来在网上查资料,发现了下面的连接 http://www.cnblogs.com/gnielee/archi ...
- POJ 3264 区间最大最小值Sparse_Table算法
题目链接:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total ...
- ZOJ 1074 To the Max
原题链接 题目大意:这是一道好题.在<算法导论>这本书里面,有一节是介绍如何求最大子序列的.这道题有点类似,区别是从数组变成了矩阵,求最大子矩阵. 解法:完全没有算法功底的人当然不知道最大 ...
- ubuntu14.04 and ros indigo install kinect driver--16
摘要: 原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/ 今日多次测设ros indigo install kinect driver ,提示各种失败,然 ...