Python中mechanize模塊練習(xí)

Part 1

An introduction about mechanize

import mechanize
br = mechanize.Browser()
br.open("http://www.example.com/")
<response_seek_wrapper at 0x109db3170 whose wrapped object = <closeable_response at 0x109d4a5f0 whose fp = <socket._fileobject object at 0x109d386d0>>>
response1 = br.follow_link()
assert br.viewing_html()
print br.title()
IANA — IANA-managed Reserved Domains
print response1.geturl()
https://www.iana.org/domains/reserved
print response1.info() #headers
Date: Fri, 13 Jul 2018 14:51:52 GMT
X-Frame-Options: SAMEORIGIN
Referrer-Policy: origin-when-cross-origin
Content-Security-Policy: upgrade-insecure-requests
Vary: Accept-Encoding
Last-Modified: Tue, 21 Jul 2015 00:49:48 GMT
Cache-control: public, s-maxage=900, max-age=7202
Expires: Fri, 13 Jul 2018 16:51:52 GMT
Content-Type: text/html; charset=UTF-8
Server: Apache
Strict-Transport-Security: max-age=48211200; preload
X-Cache-Hits: 18
Accept-Ranges: bytes
Content-Length: 10225
Connection: close
content-type: text/html; charset=utf-8
print response1.read() #body

To get the response code from a website, you can the response.code

Part 2

An example to show how to search data in mechanize

from mechanize import Browser
browser = Browser()
browser.set_handle_robots(False) #ignore the robots.txt
response = browser.open("https://www.google.com")
print response.code
200

get all forms from the website

import mechanize
br = mechanize.Browser()
br.set_handle_robots(False) #ignore the robots.txt
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')] # Google demands a user-agent that isn't a robot
br.open("https://www.google.com")
<response_seek_wrapper at 0x1081a4248 whose wrapped object = <closeable_response at 0x10819def0 whose fp = <socket._fileobject object at 0x108148a50>>>
for f in br.forms():
    print f
<f GET https://www.google.com/search application/x-www-form-urlencoded
  <HiddenControl(hl=en) (readonly)>
  <HiddenControl(source=hp) (readonly)>
  <HiddenControl(biw=) (readonly)>
  <HiddenControl(bih=) (readonly)>
  <TextControl(q=)>
  <SubmitControl(btnG=Google Search) (readonly)>
  <SubmitControl(btnI=I'm Feeling Lucky) (readonly)>
  <HiddenControl(gbv=1) (readonly)>>

Select the search box and search for 'foo'

By default 'f' would represent the name of the form. 'q' would be one of the inputs of the form whose name is set to 'q'

br.select_form('f')
br.form['q'] = 'foo'

get the search results

br.submit()
<response_seek_wrapper at 0x109738ea8 whose wrapped object = <closeable_response at 0x10819dcb0 whose fp = <socket._fileobject object at 0x108148c50>>>

Find the link to foofighters.com

resp = None
import re
for link in br.links():
    siteMatch = re.compile('https://foofighters.com/').search(link.url)
    if siteMatch:
        resp = br.follow_link(link)
        break

print the site

content = resp.get_data()
print content

Part 3 Cheart Sheet

mechanize cheat sheet

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容