导入需要的库importrequestsimportpandasaspdromlxmlimportetree各区抓取链接url='http://www.jkl.com.cn/cn/shop.aspx'headers={'User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,如Gecko)Chrome/84.0.4147.135Safari/537.36'}response=requests.get(url,headers=headers).texthtml=etree.HTML(response)city_name=html.xpath('//div[@class="infoLis"]//a/text()')city_name=[i.strip()foriincity_name]city_url=html.xpath('//div[@class="infoLis"]//a/@href')city_url=['http://www.jkl.com.cn/cn/'+iforiincity_url]3.只有一大片区域时翻页时foriincity_url:ifi=='http://www.jkl.com.cn/cn/shopLis.aspx?id=865':forainrange(1,4):date={'__EVENTTARGET':'AspNetPager1','__EVENTARGUMENT':a}response3=requests.post(url=i,data=date,headers=headers).texthtml2=etree.HTML(response3)city_shop_name=html2.xpath('//跨度[@类="con01"]/text()')city_shop_dis=html2.xpath('//span[@class="con02"]/text()')city_shop_phone=html2.xpath('//span[@class="con03"]/text()')city_shop_time=html2.xpath('//span[@class="con04"]/text()')shop_name=[d.strip()fordincity_shop_name]print(shop_name)print('*'*30)date=pd.DataFrame({"店铺名称":shop_name,"店铺地址":city_shop_dis,"联系方式":city_shop_phone,"营业时间":city_shop_time})date.to_csv("e:/CrawlCrawl[京客隆超市]门店信息.csv",index=False,header=0,mode="a",encoding="ANSI")else:response1=requests.post(url=i,headers=headers).texthtml1=etree.HTML(response1)city_shop_name1=html1.xpath('//span[@class="con01"]/text()')city_shop_dis1=html1.xpath('//span[@class="con02"]/text()')city_shop_phone1=html1.xpath('//span[@class="con03"]/text()')city_shop_time1=html1.xpath('//span[@class="con04"]/文本()')shop_name1=[c.strip()forcincity_shop_name1]print(shop_name1)#datastoragedate=pd.DataFrame({"店铺名称":shop_name1,"店铺地址":city_shop_dis1,"联系方式":city_shop_phone1,"商家信息"time":city_shop_time1})date.to_csv("e:/抓取【京客隆超市】门店信息.csv",index=False,header=0,mode="a",encoding="ANSI")完成代码#抓取[京客隆超市]店铺信息导入请求importpandasasppdfromlxmlimportetreeurl='http://www.jkl.com.cn/cn/shop.aspx'headers={'User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,如Gecko)Chrome/84.0.4147.135Safari/537.36'}response=requests.get(url,headers=headers).texthtml=etree.HTML(response)city_name=html。xpath('//div[@class="infoLis"]//a/text()')city_name=[i.strip()foriincity_name]city_url=html.xpath('//div[@class="infoLis"]//a/@href')city_url=['http://www.jkl.com.cn/cn/'+iforiincity_url]foriincity_url:ifi=='http://www.jkl.com.cn/cn/shopLis.aspx?id=865':对于一个范围(1,4):date={'__EVENTTARGET':'AspNetPager1','__EVENTARGUMENT':a}response3=requests.post(url=i,data=date,headers=headers).texthtml2=etree.HTML(response3)city_shop_name=html2.xpath('//span[@class="con01"]/text()')city_shop_dis=html2.xpath('//span[@class="con02"]/text()')city_shop_phone=html2.xpath('//span[@class="con03"]/text()')city_shop_time=html2.xpath('//span[@class="con04"]/text()')shop_name=[d.strip()fordincity_shop_name]print(shop_name)print('*'*30)date=pd.DataFrame({"店铺名称":shop_name,"店铺地址":city_shop_dis,"联系方式":city_shop_phone,"营业时间":city_shop_time})date.to_csv("e:/抓取【京客隆超市】门店信息.csv",index=False,header=0,mode="a",encoding="ANSI")else:response1=requests.post(url=i,headers=headers).texthtml1=etree.HTML(response1)city_shop_name1=html1.xpath('//span[@class="con01"]/text()')city_shop_dis1=html1.xpath('//span[@class="con02"]/text()')city_shop_phone1=html1.xpath('//span[@class="con03"]/text()')city_shop_time1=html1.xpath('//span[@class="con04"]/text()')shop_name1=[c.strip()forcincity_shop_name1]print(shop_name1)#datastoragedate=pd.DataFrame({"店铺名称":shop_name1,"店铺地址":city_shop_dis1,"联系方式":city_shop_phone1,"营业时间":city_shop_time1})date.to_csv("e:/抓取【京客隆超市】门店信息.csv",index=False,header=0,mode="a",encoding="ANSI")#如果有多个门店区域一页,而且只有一页
