前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python笔记:用Python实现简单的爬虫

python笔记:用Python实现简单的爬虫

作者头像
超级大猪
发布2019-11-22 11:45:18
4990
发布2019-11-22 11:45:18
举报

示例

做了一个简单的爬虫。使用python3。 涉及到代理的使用。关闭ssl验证。返回json的处理。 功能:用来查火车票。

import urllib.request
import json
import codecs
import time,datetime
import ssl

ssl._create_default_https_context = ssl._create_unverified_context

def GetInfo():
    while True:
        try:
            proxy_handler = urllib.request.ProxyHandler({'https': 'http://y003460:password@172.18.32.221:8080'})
            opener = urllib.request.build_opener(proxy_handler)
            urllib.request.install_opener(opener)

            resp=urllib.request\
            .urlopen('https://kyfw.12306.cn/otn/leftTicket/queryT?'
                     'leftTicketDTO.train_date=2016-10-01'
                     '&leftTicketDTO.from_station=SZQ&leftTicketDTO.to_station=LDQ&'
                     'purpose_codes=ADULT',timeout=8)

            reader = codecs.getreader("utf-8")
            train_result = json.load(reader(resp))

            # print(train_result)
            train_datas = train_result['data']
            for item in train_datas:
                train_single_data = item['queryLeftNewDTO']
                print(train_single_data['station_train_code'],"二等",train_single_data['ze_num'])
                if train_single_data['ze_num'] != "无" and train_single_data['ze_num'] != "-":
                    return
            nowtime = datetime.datetime.now()
            print(nowtime.strftime("%Y-%m-%d %H:%M:%S-%f"))
            time.sleep(8)
        except Exception as errors:
            print("一个错误",errors)

GetInfo()
print("找到了")

技术

获取网页

py2

proxy_handler = urllib2.ProxyHandler({})
opener = urllib2.build_opener(proxy_handler)
urllib2.install_opener(opener)

# download text
req = URL.format(args[1])
res_data = urllib2.urlopen(req)
res = res_data.read()
res = res.decode("utf-8")

py3

proxy_handler = urllib.request.ProxyHandler({})
opener = urllib.request.build_opener(proxy_handler)
urllib.request.install_opener(opener)
# download text
resp = urllib.request.urlopen(URL.format(args[1]))
reader = codecs.getreader("utf-8")
res = reader(resp).read()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-09-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 示例
  • 技术
    • 获取网页
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档