Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.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将代码合并到for循环中_Python_Loops_Beautifulsoup - Fatal编程技术网

如何使用Python将代码合并到for循环中

如何使用Python将代码合并到for循环中,python,loops,beautifulsoup,Python,Loops,Beautifulsoup,编辑:以下是我尝试使用的代码: from bs4 import BeautifulSoup import re import sys m = re.compile("^\d\d:\d\d$") readfile = open("C:\\Temp\\LearnPythonTheCompletePythonProgrammingCourse_Udemy.htm", 'r').read() soup = BeautifulSoup(readfile, "html.parser") ci_detail

编辑:以下是我尝试使用的代码:

from bs4 import BeautifulSoup
import re
import sys
m = re.compile("^\d\d:\d\d$")
readfile = open("C:\\Temp\\LearnPythonTheCompletePythonProgrammingCourse_Udemy.htm", 'r').read()
soup = BeautifulSoup(readfile, "html.parser")

ci_details = soup.findAll("span",{"class":"ci-details"})

timeList = []
for detail in ci_details:
    for span in detail.findAll("span"):
        if m.match(span.text):
            timeList.append(span.text)


print (timeList)

for i in timeList:
    time1=timeList[0]
    print(time1)
编辑我意识到我正在告诉Python为timeList中的每个项目打印time1。如何迭代时间列表


我想使用dstubeda的代码获取列表中的每个条目,将其转换为原始秒数,然后将它们相加。一旦完成,我将把它们转换成h:m:s。我的for循环哪里出了问题?

如果您展示代码会更容易,但您应该能够从以下内容中找到答案:

totalTime = 0
spans = soup.find_all('span',{"class":"ci-details"})
for span in spans:
     rawTime = span.text
     processedTime = DavesTimeFunction(rawTime)
     totalTime += processedTime

print("The total time is: " + str(totalTime))

如果你展示你的代码会更容易,但是你应该能够从以下几点中找到答案:

totalTime = 0
spans = soup.find_all('span',{"class":"ci-details"})
for span in spans:
     rawTime = span.text
     processedTime = DavesTimeFunction(rawTime)
     totalTime += processedTime

print("The total time is: " + str(totalTime))

为什么要下载时间而不是阅读系统时间看起来时间是讲座的长度html@wilbur这正是我所想的,但随后提出了一个问题:为什么要使用屏幕抓取而不是通过db或api访问源代码。您是否尝试过可能的复制:。公认的答案建议您使用
lxml
及其
xpath
功能。我认为这对你来说是最好的解决方案。为什么要下载时间而不是阅读系统时间呢html@wilbur这正是我所想的,但随后提出了一个问题:为什么要使用屏幕抓取而不是通过db或api访问源代码。您是否尝试过可能的复制:. 公认的答案建议您使用
lxml
及其
xpath
功能。我认为这对你来说是最好的解决办法。