第二个python代码bs4并将数据保存到文本文件 - python

我已经设法编写了第二条python代码,并且试图从网页(例如www.ksmnet.org)上的表中提取数据。我需要表格的第二列的数据,这是今天的日期,并且我设法提取了该数据。但是,我需要将数据中第一列的数据保存为包含第二列数据的文本文件。因此,例如,如果Fajr为05:00,那么我需要将文本文件另存为Fajr.txt,在此文本文件中,我需要05:00。

我知道有些时候不带“:”符号,我需要进行转换。因此,例如06.00必须是06:00。

这是我的代码:

# import libraries
import json
import urllib.request
#import soupsieve
from bs4 import BeautifulSoup
import requests

url = 'https://www.ksmnet.org/'
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
html = response.read()
soup = BeautifulSoup(html.decode("utf-8"), "html.parser")
path = '/srv/docker/homeassistant/prayer/'


table = soup.find('div', id={'prayer': 'listing sortable'})
package = '' ; version = ''
for i in table.select('tr'):
    data = i.select('td')
    if data:
        package = data[0].text.strip()
        version = ' '.join(data[1].text.strip().split())
        print(version)

谁能帮忙吗?

谢谢

python大神给出的解决方案

这是写入文本文件的代码。

import urllib.request
#import soupsieve
from bs4 import BeautifulSoup
import requests

url = 'https://www.ksmnet.org/'
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
html = response.read()
soup = BeautifulSoup(html.decode("utf-8"), "html.parser")
path = '/srv/docker/homeassistant/prayer/'


table = soup.find('div', id={'prayer': 'listing sortable'})
package = '' ; version = ''
for i in table.select('tr'):
    data = i.select('td')
    if data:
        package = data[0].text.strip()
        file = open(package[:-1] +".txt", "w+")
        version =data[1].text.strip().replace('.',':')
        file.write(version)
        file.close()

或者,您可以使用python熊猫。

import pandas as pd
url = 'https://www.ksmnet.org/'
df=pd.read_html(url)[0]
for pkg, version in zip(df['Date'],df['06/01']):
    file = open(pkg[:-1] +".txt", "w+")
    version =version.replace('.',':').strip()
    file.write(version)
    file.close()

更新校验位

import urllib.request
#import soupsieve
from bs4 import BeautifulSoup
import requests

url = 'https://www.ksmnet.org/'
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
html = response.read()
soup = BeautifulSoup(html.decode("utf-8"), "html.parser")
path = '/srv/docker/homeassistant/prayer/'


table = soup.find('div', id={'prayer': 'listing sortable'})
package = '' ; version = ''
for i in table.select('tr'):
    data = i.select('td')
    if data:
        package = data[0].text.strip()
        file = open(package[:-1] +".txt", "w+")
        version =data[1].text.strip().replace('.',':')

        #Check hours
        checkHours = version.split(':')[0]
        if len(checkHours) < 2:
            version ="0" + str(checkHours) +':' + version.split(':')[-1]
           # print(version)
        #Check minutes
        checkMinute = version.split(':')[-1]
        if len(checkMinute) < 2:
            version = version.split(':')[0] + ":" + "0" + str(checkMinute)
            print(version)

        file.write(version)
        file.close()

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

字符串文字中的正斜杠表现异常 - python

为什么S1和S2在撇号位置方面表现不同?S1="1/282/03/10" S2="4/107/03/10" R1="".join({"N\'" ,S1,"\'" }) R2="".join({"N\'…

Python:同时在for循环中添加到列表列表 - python

我想用for循环外的0索引值创建一个新列表,然后使用for循环添加到相同的列表。我的玩具示例是:import random data = ['t1', 't2', 't3'] masterlist = [['col1', 'animal1', 'an…

在Flask中测试文件上传 - python

我在Flask集成测试中使用Flask-Testing。我有一个表单,该表单具有我要为其编写测试的徽标的文件上传,但是我不断收到错误消息:TypeError: 'str' does not support the buffer interface。我正在使用Python3。我找到的最接近的答案是this,但是它对我不起作用。这是我的许多尝…

Python查找单词可以用字符构建 - python

Closed. This question needs details or clarity。它当前不接受答案。 想改善这个问题吗?添加详细信息并通过editing this post阐明问题。 4个月前关闭。 Improve this question 我想找出单词'apple'(word_list)是否可以用char_list1构建但不能用char_li…