属性をスクラップしたいURLのリストがあります。初心者からPythonへ、だから申し訳ありません。 Windows 7,64ビット。 Python 3.2。Python - HTTPResponseオブジェクトに対するURLの受け渡し
次のコードが機能します。 pblistは、キー 'short_url'を含むdictsで構成されたリストです。
次のコードは、たとえば、コードがBeautifulSoupオブジェクトにhead属性とtitle属性を持たないと主張しています。
for j in pblist[0:10]:
base_url = j['short_url']
page = urllib.request.urlopen(base_url)
if hasattr(BeautifulSoup(page), 'head') and \
hasattr(BeautifulSoup(page).head, 'title'):
print("Has head, title attributes.")
try:
j['title'] = BeautifulSoup(urllib.request.urlopen(base_url)).head.title.string.encode('utf-8')
except AttributeError:
print("Encountered attribute error on page, ", base_url)
j['title'] = "Attribute error."
pass
なぜですか? BeautifulSoup内のurllib.request.urlopenにURLを渡して、urllib.request.urlopenが返すHTTPResponseオブジェクトを渡すことの違いは何ですか?
それを手に入れました。ありがとうアンバー。 – Zack