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的更多相关文章
随机推荐
- 【Tsinghua OJ】循环移位(Cycle)
Description Cycle shifting refers to following operation on the sting. Moving first letter to the en ...
- HDU 5086
http://acm.hdu.edu.cn/showproblem.php?pid=5086 求所有连续区间的数字和 本质是一个乘法原理,当前位置的数字出现次数=这个数之前的数字个数*这个数之后的数字 ...
- HDU 5050
http://acm.hdu.edu.cn/showproblem.php?pid=5050 大数gcd import java.io.* ; import java.math.* ; import ...
- magento memcache缓存配置
在app/etc/local.xml <global>配置段中添加 cache段配置 <config> <global> <install> <d ...
- Word Ladder II
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- 怎样知道linux是否安装了mysql
1. 使用 [root@localhost Desktop]# rpm -qa |grep mysql (-qa查询已经安装的软件) mysql-libs-5.1.71-1.el6.x86_64 结果 ...
- (实用篇)PHP不用递归遍历目录下所有文件的代码
<?php /** * PHP 非递归实现查询该目录下所有文件 * @param unknown $dir * @return multitype:|multitype:string */ fu ...
- Linux启动流程详解
在BIOS阶段,计算机的行为基本上被写死了,可以做的事情并不多:一般就是通电.BIOS.主引导记录.操作系统这四步.所以我们一般认为加载内核是linux启动流程的第一步. 第一步.加载内核 操作系统接 ...
- HOG特征(Histogram of Gradient)总结(转载)
整理一下我个人觉得比较好的HOG博文 博文1:OpenCV HOGDescriptor: 参数与图解 http://blog.csdn.NET/raodotcong/article/details/6 ...
- String的常规使用集合
今天先附上代码君: package com.jacob.javase; import java.io.UnsupportedEncodingException; /* *探讨String: * * ...