【Python爬虫】利用爬虫抓取双色球开奖号码,获取完整数据,简洁45行代码实现,更新时间2023-06-28
import requests
from bs4 import BeautifulSoup
import pandas as pd
def get_ssq_data(url):
res = requests.get(url)
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text, 'html.parser')
data_list = soup.select('#tdata > tr')
ssq_data = [[item.text.strip() for item in data.select('td')] for data in data_list]
return ssq_data
def save_to_csv(ssq_data, file_name):
df = pd.DataFrame(ssq_data)
df.to_csv(file_name, index=False)
def main():
url = 'http://kaijiang.zhcw.com/zhcw/html/ssq/list_1.html'
ssq_data = get_ssq_data(url)
save_to_csv(ssq_data, 'ssq_data.csv')
if __name__ == '__main__':
main()
这段代码实现了从指定的网页抓取双色球开奖数据,并将其保存到CSV文件中。代码简洁,注重实现功能,不包含额外的错误处理。
评论已关闭