python 网络爬虫 爬取Lply分类目录网

爬取网络:Lply分类目录网
代码:

##爬取网站:Lply分类目录网(http://www.lply.net/category/)

import requests
import re
import xlwt

heads = {
     'Connection': 'keep-alive',
    'Accept-Language': 'zh-CN,zh;q=0.9',
     'Accept': 'text/html,application/xhtml+xml,application/xml;\
     q=0.9,image/webp,image/apng,*/*;q=0.8',
     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36\
    (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
}  ##heads非必要

response = requests.get('http://www.lply.net/category/', headers = heads)##此处可以省略“headers = heads”
content = response.text
pattern = re.compile('<li>.*?<a.*?href="(.*?)".*?>.*?(.*?)</a>.*?<em>.*?(\d+).*?</em>.*?</li>',re.S)
results = re.findall(pattern,content)
headee = ["序号","网站名称","包含数量","网站地址"]

####将结果存储为excel表,若执行此步,请在idle环境运行,若用pycharm,可能有其他要求
with open(r"C:\Users\AdamCY\Desktop\wenjian\python爬虫\py_lply.xlsx", "w",encoding='utf-8') as file:   
    file = xlwt.Workbook(encoding = 'utf-8')  
    sheet = file.add_sheet('sheet_1')  
    sheet.write(0, 0, headee[0]) 
    sheet.write(0, 1, headee[1])
    sheet.write(0, 2, headee[2])
    sheet.write(0, 3, headee[3])
    s = 1
    i = 0
    for result in results:
        url = "http://www.lply.net/category"+str(result[0])
        sheet.write(i+1, 0, s)
        sheet.write(i+1, 1, result[1])  
        sheet.write(i+1, 2, result[2])
        sheet.write(i+1, 3, url)
        s += 1
        i += 1
file.save(r"C:\Users\AdamCY\Desktop\wenjian\python爬虫\py_lply.xlsx")
print("excel数据保存成功")

    
####若感觉存储为excel表很麻烦,也可以执行下面代码直接打印结果        
##for result in results:
##    ur0, name, num = result
##    url = "http://www.lply.net/category"+str(ur0)
##    print(url, name, num)


####也可以选择将数据保存在txt文本中
##f = open(r'C:\Users\AdamCY\Desktop\wenjian\python爬虫\py_play.txt','w')
##for result in results:
##    url = "http://www.lply.net/category"+str(result[0])
##    f.writelines([result[1],'\t', result[2],'\t',url,'\n'])
##print("TXT文本数据保存成功")
##f.close()

三种输出结果(自选其一:excel表、直接打印、txt文本)
excel输出格式

直接打印结果
文本输出

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高山莫衣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值