Python美化组无输出
我正在通过Python测试BeautifulSoup,当我打印这段代码时,我只看到这个“[]”符号:Python美化组无输出,python,beautifulsoup,Python,Beautifulsoup,我正在通过Python测试BeautifulSoup,当我打印这段代码时,我只看到这个“[]”符号: import requests from bs4 import BeautifulSoup page = requests.get('https://www.theweathernetwork.com/ca/weather/british-columbia/vancouver') soup = BeautifulSoup(page.content, 'html.parser') week =
import requests
from bs4 import BeautifulSoup
page = requests.get('https://www.theweathernetwork.com/ca/weather/british-columbia/vancouver')
soup = BeautifulSoup(page.content, 'html.parser')
week = soup.find(id='seven-day-periods')
# print(week)
print(week.find_all('li'))
任何帮助都将不胜感激。谢谢大家! 从周内容中可以看出,没有li:
<div class="sevenDay" id="seven-day-periods">
<!-- Legend: show only when data is loaded -->
<div class="wx_legend wx_legend_hidden">
<div class="wxRow wx_detailed-metrics">
<div class="legendColumn">Feels like</div>
</div>
<div class="wxRow wx_detailed-metrics daytime">
<div class="legendColumn">Night</div>
</div>
<div class="wxRow wx_detailed-metrics nighttime">
<div class="legendColumn">Day</div>
</div>
<div class="wxRow wx_detailed-metrics">
<div class="legendColumn">POP</div>
</div>
<div class="wxRow wx_detailed-metrics">
<div class="legendColumn">Wind ()</div>
</div>
<div class="wxRow wx_detailed-metrics">
<div class="legendColumn">Wind gust ()</div>
</div>
<div class="wxRow wx_detailed-metrics daytime">
<div class="legendColumn">Hrs of Sun</div>
</div>
<div class="top-divider2"> </div>
</div>
<div class="divTableBody">
</div>
因此,现在您的代码将是
import requests
from bs4 import BeautifulSoup
page = requests.get('https://www.theweathernetwork.com/ca/weather/british-columbia/vancouver')
soup = BeautifulSoup(page.content, 'html.parser')
week = soup.find(id='seven-day-periods')
for x in week.find_all('div','legendColumn'):
print(x.findAll(text=True))
输出在哪里
[“感觉像”]
[“晚上”]
[“天”]
['POP']
[“风”]
[“阵风”]
['Hrs of Sun']如果您得到一个空列表,这意味着没有包含li元素的id“七天周期”。七天周期可能没有任何列表谢谢,但即使我尝试每周。查找所有“类”,它也会显示空类,因此我不确定问题出在哪里。非常感谢。即使我尝试week.find_all'class',它也会显示空列表,但列被创建为'class',所以我有点困惑。@James很乐意帮忙!祝你好运
import requests
from bs4 import BeautifulSoup
page = requests.get('https://www.theweathernetwork.com/ca/weather/british-columbia/vancouver')
soup = BeautifulSoup(page.content, 'html.parser')
week = soup.find(id='seven-day-periods')
for x in week.find_all('div','legendColumn'):
print(x.findAll(text=True))