我正在为一个网站编写一个 Python 解析器来自动完成一些工作,但我不太喜欢 Py 的“re”模块(正则表达式),并且无法使其工作。
req = urllib2.Request(tl2)
req.add_unredirected_header('User-Agent', ua)
response = urllib2.urlopen(req)
try:
html = response.read()
except urllib2.URLError, e:
print "Error while reading data. Are you connected to the interwebz?!", e
soup = BeautifulSoup.BeautifulSoup(html)
form = soup.find('form', id='form_product_page')
pret = form.prettify()
print pret
Result:
<form id="form_product_page" name="form_1362737440" action="/download/791055/164084/" method="get">
<input id="nojssubmit" type="submit" value="Download" />
</form>
事实上,代码已经完成,正是我开始所需要的。现在,我想知道应该以哪种方式从“form”标签中提取“action”属性。这只是我需要从 BeautifulSoup 响应中得到的。
我尝试过使用form = soup.find('form', id='form_product_page').parent.get('action')
但结果是“无”。我想要提取的是例如“/download/791055/164084/”。这在链接的每个 URL 上都是不同的。
Variables (example):
tl2 =
http://example.com http://example.com
ua = Mozilla Firefox / 14.04
您可以一步完成:
action = soup.find('form', id='form_product_page').get('action')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)