gpt4 book ai didi

python - 网页抓取打印换行符

转载 作者:行者123 更新时间:2023-12-01 08:08:04 24 4
gpt4 key购买 nike

我希望我的网络抓取工具的输出是每个结果的字符串。我希望数字之间用逗号分隔。

代码:

import requests
from bs4 import BeautifulSoup
from datetime import datetime

response = requests.get('https://www.lotterycorner.com/mi/mega-millions/2019')
soup = BeautifulSoup(response.text, 'html.parser')
date = soup.find_all("td", {"class":"win-nbr-date col-sm-3 col-xs-4"})
for ultag in soup.find_all("ul",{"class":"nbr-grp"}):
for litag in ultag.find_all('li'):
results = (litag.get_text().replace(' ','').replace('MegaBall',''))
print(results)
for date, results in zip(date,results):
date2 = (date.get_text())
date = (datetime.strptime(date2, '%b %d, %Y'))
MegaMillions2019 = (date.strftime("%m%d%Y")+(','))
print(MegaMillions2019)

输出:

5
14
15
62
66
3
巨型倍增器3X
4
14
22
43
58
9
巨型倍增器3X
7
36
58
60
62
10
Megaplier3X

我希望输出是:

5,14,15,62,66,3
4,14,22,43,58,9
7,36,58,60,62,10

因此,将数据放在一个字符串中,而不是相互堆叠,然后从字符串末尾删除 megaplier(integer)X。

通过添加此代码,我已经摆脱了 megaplier。

results2 = (results.replace('Megaplier2X','').replace('Megaplier3X','').replace('Megaplier4X','').replace('Megaplier5X',''))
print(results2)

最佳答案

您可以像这样解析数据:

import requests
from bs4 import BeautifulSoup
from datetime import datetime

response = requests.get('https://www.lotterycorner.com/mi/mega-millions/2019')
soup = BeautifulSoup(response.text, 'html.parser')
date = soup.find_all("td", {"class":"win-nbr-date col-sm-3 col-xs-4"})

data = []
for ultag in soup.find_all("ul",{"class":"nbr-grp"}):
for litag in ultag.find_all('li'):
results = (litag.get_text().replace(' ','').replace('MegaBall',''))
data.append(results)

parsed = []
for i in range(int(len(data)/7)):
j = i*7
parsed.append(data[j:j+6])

text = '\n'.join([', '.join(parsed[i]) for i in range(len(parsed))])
print(text)

输出:

5, 14, 15, 62, 66, 3
4, 14, 22, 43, 58, 9
7, 36, 58, 60, 62, 10
10, 42, 53, 67, 68, 15
3, 29, 56, 62, 64, 4
10, 12, 16, 49, 57, 18
4, 9, 42, 62, 68, 7
15, 20, 32, 37, 52, 6
29, 33, 39, 60, 66, 21
10, 12, 14, 24, 60, 20
18, 24, 31, 34, 55, 4
17, 24, 34, 56, 65, 3
10, 38, 40, 43, 65, 12
15, 32, 39, 50, 65, 7
14, 24, 31, 42, 48, 13
3, 34, 36, 59, 66, 7
2, 37, 48, 66, 68, 11
10, 33, 53, 54, 62, 22
8, 16, 30, 38, 61, 10
4, 15, 37, 59, 64, 16
2, 43, 48, 62, 64, 24
29, 52, 58, 60, 62, 7
4, 5, 31, 62, 69, 20
13, 26, 29, 38, 64, 5
21, 29, 35, 54, 60, 15
34, 44, 57, 62, 70, 14

parsed 包含列表中的数据,这可能更有用:

print(parsed)


[['5', '14', '15', '62', '66', '3'], ['4', '14', '22', '43', '58', '9'], ['7', '36', '58', '60', '62', '10'], ['10', '42', '53', '67', '68', '15'], ['3', '29', '56', '62', '64', '4'], ['10', '12', '16', '49', '57', '18'], ['4', '9', '42', '62', '68', '7'], ['15', '20', '32', '37', '52', '6'], ['29', '33', '39', '60', '66', '21'], ['10', '12', '14', '24', '60', '20'], ['18', '24', '31', '34', '55', '4'], ['17', '24', '34', '56', '65', '3'], ['10', '38', '40', '43', '65', '12'], ['15', '32', '39', '50', '65', '7'], ['14', '24', '31', '42', '48', '13'], ['3', '34', '36', '59', '66', '7'], ['2', '37', '48', '66', '68', '11'], ['10', '33', '53', '54', '62', '22'], ['8', '16', '30', '38', '61', '10'], ['4', '15', '37', '59', '64', '16'], ['2', '43', '48', '62', '64', '24'], ['29', '52', '58', '60', '62', '7'], ['4', '5', '31', '62', '69', '20'], ['13', '26', '29', '38', '64', '5'], ['21', '29', '35', '54', '60', '15'], ['34', '44', '57', '62', '70', '14']]

关于python - 网页抓取打印换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55434690/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com