Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python美化组无输出_Python_Beautifulsoup - Fatal编程技术网

Python美化组无输出

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 =

我正在通过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 = 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))