python爬虫好久没用都生疏了,刚好朋友要排列三数据,就用这个练手,
去体彩官网http://www.lottery.gov.cn/historykj/history.jspx?_ltype=pls
不闲聊,直接上代码
import requests
from requests.exceptions import RequestException
import csv
from bs4 import BeautifulSoup as bs
数据存储到csv
def write_to_file(item):
file_name = ‘PLS.csv’
# ‘a’为追加模式(添加)
# utf_8_sig格式导出csv不乱码
with open(file_name, ‘a’, encoding=‘utf_8_sig’, newline=’’) as f:
fieldnames = [‘期号’, ‘中奖号码’, ‘开奖日期’]
w = csv.DictWriter(f, fieldnames=fieldnames)
w.writerow(item)
def get_page(i):
try:
# 这是一个UA伪装,告诉网站你浏览器和操作系统系统
headers = {
‘User-Agent’: 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) ’
‘Chrome/66.0.3359.181 Safari/537.36’,
‘Connection’: ‘keep-alive’
}
url = "http://ww