Python 韦氏印刷新行

Python 韦氏印刷新行,python,web-scraping,printing,newline,Python,Web Scraping,Printing,Newline,我希望web scraper的输出是每个结果的字符串。我希望数字之间用逗号分隔 代码: 输出: 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

我希望web scraper的输出是每个结果的字符串。我希望数字之间用逗号分隔

代码:

输出:

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
5
14
15
62
66
3.
Megaplier3X
4.
14
22
43
58
9
Megaplier3X
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(整数)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']]

尝试以下操作以获得所需的输出。如果日期很关键,则始终可以稍后添加日期

import requests
from bs4 import BeautifulSoup

response = requests.get('https://www.lotterycorner.com/mi/mega-millions/2019')
soup = BeautifulSoup(response.text, 'html.parser')
for items in soup.select(".nbr-grp"):
    [span.extract() for span in items.select("span")]
    data = [item.get_text(strip=True) for item in items.select("li:not(.nbr-txt)")]
    print(data)
您可能获得的输出:

['5', '14', '15', '62', '66', '3']
['4', '14', '22', '43', '58', '9']
['7', '36', '58', '60', '62', '10']
['10', '42', '53', '67', '68', '15']
如果您想在没有列表的情况下获得输出,请尝试使用上面的现有列表进行替换:

data = ' '.join([item.get_text(strip=True) for item in items.select("li:not(.nbr-txt)")])