python学习2019/05/05,1.12.33

爬虫

爬虫的前奏:
1.明确目的
2.找到对应的网页
3.分析网页的结构找到数据所在的标签位置

1.模拟http请求,向服务器发送这个请求
获取到服务器返回给我们的html
2.用正则表达式提取我们要的数据

import re
from urllib import request
#断点调试 F5
class Spider():
url = ‘https://live.ixigua.com/category/1/114/’ # 调用网站
root_patten = ‘

[\S\s]*?
’ #搜索到需要的代码列

def __fetch_content(self):   #私有方法
    r = request.urlopen(Spider.url)
    #bytes   字节
    htmls = r.read()                                                    #读取
    htmls = str(htmls,encoding = 'utf-8')               #网站编码的解码
    return htmls                                                      #返回

def __analysis(self,htmls):
    root_html = re.findall(Spider.root_patten,htmls)            #查找
    anchor = []
    for html in root_html:
        name = re.findall(Spider,name_pattern,html)            #查找名字
        number = re.findall(Spider.number_pattern,html)         #查找数字
        anchor = {'name':name,'number':number}            #赋值为dict,进行输出
        anchors.append(anchor)
    print(anchors[0])
    return anchors                                   #返回值

def __refine(self,anchors):
    pass 

def go(self):                                                    #做一个返回值输出
    htmls = self.__fetch_content()
    anchors = self.__analysis(htmls)
    self.__refine(anchors)

spider = Spider()
spider.go() #返回需要的值

#失败中。。。

1.12.33

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值