2017-10-01 14 views
1

私はWebクローラーを作成してpycharmでプログラムを実行してURLのアンカータグを取得しようとしていますが、私が得た出力は入力したURLと全く同じです。コードは以下の通りである:ここで注意すべきPythonでWebクローラーを作成中にアンカータグを取得する

import urllib.request,urllib.parse,urllib.error 
    from bs4 import BeautifulSoup 
    import ssl 
    ctx=ssl.create_default_context() 
    ctx.check_hostname=False 
    ctx.verify_mode=ssl.CERT_NONE 

    url=input("https://en.wikipedia.org/wiki/Apple_Inc.") 
    html=urllib.request.urlopen(url, context=ctx).read() 
    soup=BeautifulSoup(html, 'html.parser') 

    tags=soup("a") 
    for tag in tags: 
     print(tag.get("href",None)) 

ことの一つは、urllibはライブラリにのみurllib.errorが使用声明との両方urllib.requestとして表示され、urllib.parseがどの私にはできないとして、未使用の文が表示されていることです理由を理解する。

このプログラムの出力は、https://en.wikipedia.org/wiki/Apple_Incです。

私はpython 3.5.1とpycharmコミュニティ版を使用しています。

答えて

0

本当にrequestsパッケージを使用してください。クロールの目的には非常に便利です。チェックアウトここで

this user response about requests.はあなたのコードが変換されます。

import requests 
from bs4 import BeautifulSoup 

request = requests.get("https://en.wikipedia.org/wiki/Apple_Inc.").text 
soup = BeautifulSoup(request, "html.parser") 

anchor = soup.find_all("a", href=True) 
for a in anchor: 
    print (a["href"]) 
関連する問題