在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python/ 試寫爬蟲,結(jié)果沒有出現(xiàn)網(wǎng)頁內(nèi)容

試寫爬蟲,結(jié)果沒有出現(xiàn)網(wǎng)頁內(nèi)容

題目描述

如以下程式碼,我最後想要print出soup

相關(guān)代碼

import requests
from bs4 import BeautifulSoup
def get_webpage(url):
html_page=requests.get(url)
if html_page.status_code!=200:
    print("invalid url",html_page.status_code)
    return None
else:
    return html_page.text
site="https://tw.stock.yahoo.com/q/q?s=2377"
html=get_webpage(site)
soup=BeautifulSoup(html,"html.parser")
print(soup)

你期待的結(jié)果是什么?實際看到的錯誤信息又是什么?

我期待他輸出soup
但"請按任意鍵繼續(xù) . . ."電腦這麼告訴我,並沒有顯示任何內(nèi)容

問題描述

覺得可能有什麼盲點,但是我找不出來

問題出現(xiàn)的環(huán)境背景及自己嘗試過哪些方法

我用的是visual studio 2017,python的應(yīng)用程式開發(fā),並且已下載bs4

回答
編輯回答
維他命

因為你的代碼中函數(shù)體沒有縮進,python是嚴格要求縮進的。html_page那行開始,到return html_page.text那行結(jié)束,需要縮進。下面的是改正后的:
如果你運行失敗,請把錯誤信息貼出來,這段代碼我執(zhí)行過,沒有問題。 我有點懷疑你用的Python2嗎,這段代碼要求Python版本3.0以上

import requests
from bs4 import BeautifulSoup
def get_webpage(url):
    html_page=requests.get(url)
    if html_page.status_code!=200:
        print("invalid url",html_page.status_code)
        return None
    else:
        return html_page.text
site="https://tw.stock.yahoo.com/q/q?s=2377"
html=get_webpage(site)
soup=BeautifulSoup(html,"html.parser")
print(soup)
2017年9月2日 19:05