双色球这个页面有点坑,首先是https加密链接,然后主要的是,他们页面可能编码不是用的常用编码,
用urllib的urlopen方法爬取下来的内容貌似16进制码,如下:
b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\xad[\xdb\x8e\x1b\xc7\x11}\x96\xbf\x82a\x90\xc0\x02f\xb3\xd3\xdds\x95v\xe7\xc5A~c\xc1%\xa9]&\x14\xb9 \xa9\x8b\xf3$\xc3\x08\x0c\x1b\x06l#\xb0\x13\x046\xe4\x181\x90
尝试了各种转换各种decode 无效,希望各位大佬能指点迷津。

urllib不行后换了其它的三方库,requests和httplib2两个库均可,大家有类似情况可参考:
## Available
h = httplib2.Http('.cache')
resp, content = h.request(url)
# print(content.decode())
return content.decode()

## Available
resp = requests.get(url , headers=headers , timeout= 10 , verify= False)
contents = resp.content
# print(type(contents))

# print(contents)

完整代码:

import requests
import csv
import socket
import urllib.request as req
import urllib
from bs4 import BeautifulSoup
import gzip
import ssl
import httplib2# 获取双色球往期数据
# @author puck
# @Date 2018-02-01
# https://datachart.500.com/ssq/history/history.shtml
# https://datachart.500.com/ssq/history/newinc/history.php?limit=30&sort=0def get_content(url):try:context = ssl._create_unverified_context()headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}# requset = req.Request(url, headers=headers, method='GET')# result = req.urlopen(requset, timeout=10, context=context).read() # 此方法获取的内容貌似16进制乱码# rep.encoding = 'utf-8'# return rep.text# print(type(result))# print(result.decode())## Available# h = httplib2.Http('.cache')# resp, content = h.request(url)# print(content.decode())# return content.decode()## Availableresp = requests.get(url, headers=headers, timeout=10, verify=False) # verify 参数必传,falst不校验SSL证书contents = resp.content# print(type(contents))# print(contents)if contents:return contents.decode()else:print("can't get data from url:" + url)except req.HTTPError as e:print('HTTPError:', e)except req.URLError as e:# SSL证书验证失败错误# URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:748)>print('URLError:', e)except socket.timeout as e:print('socket.timeout:', e)except BaseException as e:print('BaseException:', e)def get_data(html):if html is None:returnfinal = []bs = BeautifulSoup(html, "html.parser")print(bs)datas = bs.find("tbody", {'id': 'tdata'})trs = datas.find_all('tr')for tr in trs:temp = []tds = tr.find_all('td')temp.append(tds[0].string)temp.append(tds[1].string)temp.append(tds[2].string)temp.append(tds[3].string)temp.append(tds[4].string)temp.append(tds[5].string)temp.append(tds[6].string)temp.append(tds[7].string)final.append(temp)print(final)return finaldef write_data(data, name):with open(name, 'w', errors='ignore', newline='') as f:csvFile = csv.writer(f)csvFile.writerows(data)if  __name__  == '__main__':url = 'https://datachart.500.com/ssq/history/newinc/history.php?limit=30&sort=0'html = get_content(url)data = get_data(html)write_data(data, 'TwoColorBallData30.csv')

爬取结果:
18016,01,11,12,18,25,27,16
18015,11,15,20,21,26,33,15
18014,09,12,20,24,28,31,07
18013,06,08,13,15,22,33,06
18012,11,12,13,19,26,28,12
18011,03,10,21,23,27,33,11
18010,01,08,17,20,21,22,03
18009,05,10,17,23,26,32,07
18008,05,09,10,12,17,19,13
18007,13,14,20,25,27,31,12
18006,02,07,08,09,17,29,11
18005,02,20,21,28,31,33,06
18004,14,18,19,26,30,31,11
18003,01,14,16,17,20,31,04
18002,07,18,24,29,31,33,16
18001,01,08,11,26,28,31,04
17154,05,09,13,15,18,26,05
17153,07,11,12,13,18,19,16
17152,06,10,23,25,26,29,05
17151,02,05,07,09,11,27,16
17150,06,14,19,20,21,23,08
17149,05,08,15,20,27,30,13
17148,04,07,11,14,29,32,12
17147,03,07,20,21,25,31,14
17146,01,19,25,26,27,33,10
17145,02,06,12,17,25,28,12
17144,03,14,16,20,31,32,09
17143,04,06,09,14,20,29,14
17142,08,13,14,18,23,33,06
17141,01,06,07,11,13,15,05

以上。

Python3 多种方式爬取最新30期双色球历史数据存入csv相关推荐

  1. 用Python爬取最新股票数据含完整源代码

    用Python爬取最新股票数据含完整源代码 抓取目标: url:http://webapi.cninfo.com.cn/#/marketDataDate 数据目标: 获取 证券代码 证券简称 交易日期 ...

  2. python3网络爬虫--爬取b站用户投稿视频信息(附源码)

    文章目录 一.准备工作 1.工具 二.思路 1.整体思路 2.爬虫思路 三.分析网页 1.分析数据加载方式 2.分词接口url 3.分析用户名(mid) 四.撰写爬虫 五.得到数据 六.总结 上次写了 ...

  3. java爬虫系列第二讲-爬取最新动作电影《海王》迅雷下载地址

    为什么80%的码农都做不了架构师?>>>    1. 目标 使用webmagic爬取动作电影列表信息 爬取电影**<海王>**详细信息[电影名称.电影迅雷下载地址列表] ...

  4. python3 requests+bs4爬取某网页MM图片

    python3 requests+bs4爬取某网页MM图片 原理: 将所要抓取的首页分标题及地址保存到字典,遍历字典,对每一个标题下的所有分页进行抓取 import requests from bs4 ...

  5. python伪装浏览器爬取网页图片_【IT专家】python 分别用python2和python3伪装浏览器爬取网页内容...

    本文由我司收集整编,推荐下载,如有疑问,请与我司联系 python 分别用 python2 和 python3 伪装浏览器爬取网页内容 2017/07/06 1 python 网页抓取功能非常强大,使 ...

  6. python3爬虫:爬取电影天堂电影信息

    python3爬虫:爬取电影天堂电影信息 #爬取电影天堂电影信息 #爬取电影天堂电影信息 #爬取电影天堂电影信息 from lxml import etree import requestsBASE_ ...

  7. python3.x+requests 爬取网站遇到中文乱码的解决方案

    正常情况下,遇见问题上google找答案能甩百度100条街,但是这个问题是个例外······人家老外就没有乱码的问题.言归正传,首先建议大家看一下python3.x+requests 爬取网站遇到中文 ...

  8. python3 爬虫数据处理 爬取华为应用市场 APP应用评论(二)根据评论生成词云——小猿搜题

    python3 爬虫&数据处理 爬取华为应用市场 APP应用评论(二)根据评论生成词云--小猿搜题 # 导入扩展库 import re # 正则表达式库 import collections ...

  9. python3 爬虫数据处理 爬取华为应用市场 APP应用评论(二)根据评论生成词云——学霸君

    python3 爬虫&数据处理 爬取华为应用市场 APP应用评论(二)根据评论生成词云--学霸君 # 导入扩展库 import re # 正则表达式库 import collections # ...

  10. python3 爬虫数据处理 爬取华为应用市场 APP应用评论(二)根据评论生成词云——作业帮

    python3 爬虫&数据处理 爬取华为应用市场 APP应用评论(二)根据评论生成词云--作业帮 # 导入扩展库 import re # 正则表达式库 import collections # ...

最新文章

  1. ComponentOne Wijmo Editor 在光标处添加文本
  2. Hadoop 运行模式
  3. 解决SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问的方法...
  4. 思考题-算法小思考题
  5. 高并发 高负载 网站系统架构 !深入讨论!【转载】
  6. php返回支付状态,magento paypal返回支付状态
  7. 注意!腾讯语音助手也已抵达战场(内部不止这一个)
  8. storage theory
  9. spider_使用随机User-Agent库, 爬取笔趣阁万古天帝章节目录_(fake_useragent)
  10. 对话DDM:分布式数据库中间件全解析
  11. Java访问用户名密码验证的url
  12. WebSockt面试题
  13. 小学生听力测试软件,亲测十款小学英语APP,为了孩子请收藏
  14. 全国各地货运量(全国与各省级指标,2005-2019数据分析对比)
  15. WiMAX与Wi-Fi、DSL和3G的竞合关系
  16. Scrapy去重操作
  17. XML:Schema、三种编写Schema的方式
  18. 如何进行数据文件的传输(不简单)
  19. RabbitMQ-2-工作模式及参数配置
  20. Nofollow涵义

热门文章

  1. css经典布局——圣杯布局
  2. 【Ubuntu 安装】Ubuntu20.04和Win10双系统安装指南
  3. centos7中添加大硬盘(超过2T)分区
  4. ffmpeg视频录制
  5. ubuntu16.04使用360随身wifi2代
  6. 【数学建模】论文排版
  7. Python初学者(零基础学习Python入门)书籍、视频、资料分享
  8. 【记一下1】2022年6月29日 哥和弟 双重痛苦
  9. 一个稳定、快速的云服务器——萤光云
  10. 标准摇滚电吉他音源 Orange Tree Samples Evolution Rock Standard