美文网首页
S01E01-爬取京客隆网站店铺信息【requests获取xpa

S01E01-爬取京客隆网站店铺信息【requests获取xpa

作者: 布衣夜行人 | 来源:发表于2021-12-18 16:59 被阅读0次

    关于Spder中编译器的设置问题

    dedicated是专用的意思,console是控制台的意思

    Python中注释的写法

    1.单行注释,代码行以 # 开头 #这是一个单行注释print('hello world')
    2.多行注释,使用三个单引号,或者三个双引号将其要注释的内容扩起来

    # -*- coding: utf-8 -*-
    """
    Spyder Editor
    
    This is a temporary script file.
    """
    
    import requests
    from lxml import etree
    import pandas as pd
    
    address="http://www.jkl.com.cn/shop.aspx"
    My_agent={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36'}
    
    Page_date=requests.get(url=address,headers=My_agent).text
    jiexi_date=etree.HTML(Page_date)
    #print(jiexi_date)
    chenqu_a=jiexi_date.xpath('//div[@class="infoLis"]//@href')
    #print(chenqu_a)
    for i in chenqu_a:
        chenqu_a2="http://www.jkl.com.cn/"+i
        #print(chenque_a2)
        sonpage_date=requests.get(url=chenqu_a2,headers=My_agent).text
        jiexi_date1=etree.HTML(sonpage_date)
        shop_name=jiexi_date1.xpath('//span[@class="con01"]/text()')
        shop_adress=jiexi_date1.xpath('//span[@class="con02"]/text()')
        shop_tel=jiexi_date1.xpath('//span[@class="con03"]/text()')
        shop_opentime=jiexi_date1.xpath('//span[@class="con04"]/text()')
        #print(shop_name)
        shop_date=pd.DataFrame({'店名':shop_name,'地址':shop_adress,'电话号码':shop_tel,'营业时间':shop_opentime})
        shop_date.to_csv('d:/店铺信息1.csv',index=False,header=0,mode='a',encoding='ANSI')
    

    小结-上述代码实现思路

    应用条件:一个简单的没有涉及反爬虫技术的网站;使用request库发起请求
    1、准备工作:确定要爬取网站的url链接及自己正常访问时所需的伪装UA
    2、requests获取网址数据,etree解析网址数据,xpath选择本爬虫需要爬取的数据(在首页中找到二级页面的超链接,形成一个列表)
    3、利用一个循环补充完成所有二级页面的超链接网址,再次使用requests发起请求,获取二级页面中的主要信息。(实质在重复第2个步骤中的几个操作)
    4、利用pandas库形成结构化的数据,最终指定一个本地文件储存路径,输出数据到某个.csv文件中去。

    相关文章

      网友评论

          本文标题:S01E01-爬取京客隆网站店铺信息【requests获取xpa

          本文链接:https://www.haomeiwen.com/subject/ihfgfrtx.html