0

Mechanize(Pythonで)を使用してフォームを送信したいのですが、残念ながらページのコーディングが正しく<select>なく、要素が実際には<form>タグ内にありません。

したがって、次のフォームを使用して従来の方法を使用することはできません。

forms = [f for f in br.forms()]
mycontrol = forms[1].controls[0]

代わりに何ができますか?

これが私がスクレイプしたいページと関連するコードです-私はla選択したアイテムに興味があります:

    <fieldset class="searchField">
      <label>By region / local authority</label>
      <p id="regp">
        <label>Region</label>
        <select id="region" name="region"><option></option></select>
      </p>
      <p id="lap">
        <label>Local authority</label>
        <select id="la" name="la"><option></option></select>
      </p>
      <input id="byarea" type="submit" value="Go" />
      <img id="regmap" src="/schools/performance/img/map_england.png" alt="Map of regions in England" border="0" usemap="#England" />
    </fieldset>
4

1 に答える 1

1

これは実際にはあなたが考えるよりも複雑ですが、それでも実装は簡単です。何が起こっているのかというと、リンクしているWebページがJSONによって地方自治体を引き込んでいるということです(これが、name="la"select要素がJavascriptを欠いているMechanizeを埋めない理由です)。最も簡単な方法は、PythonでこのJSONデータを直接要求し、その結果を使用して各データページに直接移動することです。

import urllib2
import json

#The URL where we get our array of LA data
GET_LAS = 'http://www.education.gov.uk/cgi-bin/schools/performance/getareas.pl?level=la&code=0'

#The URL which we interpolate the LA ID into to get individual pages
GET_URL = 'http://www.education.gov.uk/schools/performance/geo/la%s_all.html'

def get_performance(la):
    page = urllib2.urlopen(GET_URL % la)
    #print(page.read())

#get the local authority list
las = json.loads(urllib2.urlopen(GET_LAS).read())

for la in las:
    if la != 0:
        print('Processing LA ID #%s (%s)' % (la[0], la[1]))
        get_performance(la[0])

ご覧のとおり、リンクしたページを読み込んだり、Mechanizeを使用して読み込んだりする必要はありません。ただし、学校名を解析してからパフォーマンスの数値を解析する方法が必要です。

于 2012-06-15T15:22:44.387 に答える