除了alecxe mentioned 之外,另一种选择是使用 GUI 浏览器工具,例如 Firefox 的 Web 控制台
检查单击提交按钮时发出的 POST。有时你
可以找到 POST 数据并简单地对其进行欺骗。例如,使用示例
你发布的网址,如果你
你会得到类似的东西
all
field_36[]=73
field_37[]=76
field_32[]=82
submit=Search
(请注意,Web 控制台菜单会根据您的 Firefox 版本而有所不同,因此是 YMMV。)然后您可以使用以下代码来欺骗 POST:
import urllib2
import urllib
import lxml.html as LH
url = "http://apply.ovoenergycareers.co.uk/vacancies/#results"
params = urllib.urlencode([('field_36[]', 73), ('field_37[]', 76), ('field_32[]', 82)])
response = urllib2.urlopen(url, params)
content = response.read()
root = LH.fromstring(content)
print('\n'.join([tag.text_content() for tag in root.xpath('//dl')]))
产生
Regulatory Data Analyst
Contract Type
Permanent
Contract Hours
Full-time
Location
Bristol
Department
Business Intelligence
Full description
如果您检查 HTML 并搜索 field_36[],您会发现
<div class="multiwrapper">
<p class="sidenote multi">(Hold the ctrl (pc) or cmd (Mac) keys for multi-selects) </p>
<select class="select-long" multiple size="5" name="field_36[]" id="field_36"><option value="0">- select all -</option>
<option selected value="73" title="Permanent">Permanent</option>
<option value="74" title="Temporary">Temporary</option>
<option value="75" title="Fixed-term">Fixed-term</option>
<option value="81" title="Intern">Intern</option></select>
</div>
从中很容易推测field_36[]控制Contract Type,值73对应“永久”,74对应“临时”等。同样你可以找出@的选项987654332@、field_32[] 和 all(可以是任何搜索词字符串)。如果你对 HTML 有很好的理解,你甚至可能不需要浏览器工具来构建 POST。