私は最近、Beautifulsoupで遊んでいるときにPython辞書で奇妙な問題に遭遇しました。私のコードは次のようになります。
import urllib2
from BeautifulSoup import BeautifulSoup
response = urllib2.urlopen("http://www.msn.com")
html = response.read()
soup = BeautifulSoup(html)
anchors = soup.findAll('a')
for a in anchors:
if not a.has_key('href') == 'href' in a:
print a
実際には多くのリンクが出力され、has_keyは「in」とは異なる動作をします。
誰でも説明できますかありがとうございます!