个人博客转至:tybai.com

无聊就想打码,打码使我快乐


Fork me on GitHub

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)

posted on 2016-11-07 16:27  TTyb  阅读(1592)  评论(2编辑  收藏  举报

导航


不用多久

我就会升职加薪

当上总经理

出任CEO

迎娶白富美

走上人生巅峰

Pulpit rock