我在 Vs 代碼中使用了這段代碼:
import requests
from bs4 import BeautifulSoup
url = "https://www.gov.uk/search/news-and-communications"
reponse = requests.get(url)
page = reponse.content
soup = BeautifulSoup(page, "html.parser")
class_name= "gem-c-document-list__item-link"
titres = soup.find_all("a", class_=class_name)
titres_textes=[]
for titre in titres:
titres_textes.append(titre.string)
titres_textes
但是當我嘗試在Ctrl Alt N
沒有任何反應的情況下運行它時,為什么?
python versions>3.10
extensions python on Vscode> Python ok,Django ok,Magic-python ok,code runner,python for vscode ok
pip> Latest versions currently installed
uj5u.com熱心網友回復:
使用列印并保持適當的代碼可讀性。
import requests
from bs4 import BeautifulSoup
url = "https://www.gov.uk/search/news-and-communications"
reponse = requests.get(url)
page = reponse.content
soup = BeautifulSoup(page, "html.parser")
class_name= "gem-c-document-list__item-link"
titres = soup.find_all("a", class_=class_name)
titres_textes=[]
for titre in titres:
titres_textes.append(titre.string)
print(titres_textes)
嘗試從 VS Code 終端運行您的代碼。首先轉到路徑然后鍵入命令: python filename.py
uj5u.com熱心網友回復:
同意BrutusForcus,這只是因為HTML頁面被更改了。您可以將 的值更改為其他值class_name,然后洗掉字串titre以使其作業。
比如這個:
import requests
from bs4 import BeautifulSoup
url = "https://www.gov.uk/search/news-and-communications"
reponse = requests.get(url)
page = reponse.content
soup = BeautifulSoup(page, "html.parser")
class_name = "gem-c-document-list__item-metadata"
titres = soup.find_all("ul", class_=class_name)
titres_textes = []
for titre in titres:
titres_textes.append(titre)
print(titres_textes)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/331219.html
