1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| from PyQt4 import QtGui, QtCore, QtNetwork,QtWebKit
import requests
def download(webview,url):
loop = QtCore.QEventLoop()
webview.loadFinished.connect(loop.quit)
req = QtNetwork.QNetworkRequest(QtCore.QUrl(url))
webview.load(req)
loop.exec_()
app = QtGui.QApplication([])
webview = QtWebKit.QWebView()
webview.show()
download(webview,'https://passport.baidu.com/v2/?login')
frame = webview.page().mainFrame()
frame.findFirstElement('#TANGRAM__PSP_3__userName').setAttribute('value','username')
frame.findFirstElement('#TANGRAM__PSP_3__password').setAttribute('value','password')
frame.findFirstElement('#TANGRAM__PSP_3__submit').evaluateJavascript("this.click()")
#等待结果
elements = QtWebKit.QWebElement()
while elements.isNull():
#每次循环,都会调用app.processEvents(),用于给Qt事件循环执行任务的时间,比如响应点击事件和更新GUI
app.processEvents()
elements = frame.findFirstElement('#displayUsername')
s_COOKIEs = {}
for citem in webview.page().networkAccessManager().COOKIEJar().COOKIEsForUrl(QtCore.QUrl('http://www.baidu.com')):
s_COOKIEs[bytes(citem.name()).decode()] = bytes(citem.value()).decode()
webview.close()
s = requests.Session()
s.headers = {
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding':'gzip, deflate, sdch',
'Accept-Language':'zh-CN,zh;q=0.8',
'Connection':'keep-alive',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36',
}
s.COOKIEs.update(s_COOKIEs)
r = s.get('http://tieba.baidu.com/p/5007359263')
print(r.status_code)
with open('tieba.html','w') as f:
f.write(r.text) |