python3_mechanicalsoup
代码语言:javascript
复制# !/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)