首页 > 其他分享 >网络爬虫以及自动化办公基础

网络爬虫以及自动化办公基础

时间:2022-10-26 21:34:41浏览次数:45  
标签:comp list 爬虫 办公 模块 自动化 pip home data

Day22网络爬虫以及自动化办公基础

  1. 作业讲解
  2. 第三方模块下载与使用
  3. 网络爬虫模块之request模块
  4. 网络爬虫实践之爬取链家二手房数据
  5. 自动化办公领域openpyx

今日内容详细

1.作业剖析
# 网址: http://www.redbull.com.cn/about/branch

import re

comp_title_list = re.findall('<h2>(.*?)</h2>', data)
comp_address_list = re.findall("<p class='mapIco'>(.*?)</p>", data)
comp_email_list = re.findall("<p class='mailIco'>(.*?)</p>", data)
comp_phone_list = re.findall("<p class='telIco'>(.*?)</p>", data)
res = zip(comp_title_list, comp_address_list, comp_email_list, comp_phone_list)
with open(r'comp_info.txt', 'w', encoding='utf8') as f:
    for data_tuple in res:
        print(
            """
            公司名称:%s
            公司地址:%s
            公司邮编:%s
            公司电话:%s
            """ % data_tuple)
        f.write( """
            公司名称:%s
            公司地址:%s
            公司邮编:%s
            公司电话:%s\n
            """ % data_tuple)
2.第三方模块下载与使用
1.pip工具
注意每个解释器都有pip工具 如果我们的电脑上有多个版本的解释器那么我们在使用pip的时候一定要注意到底用的是哪一个 否则极其任意出现使用的是A版本解释器然后用B版本的pip下载模块
       
    为了避免pip冲突 我们在使用的时候可以添加对应的版本号
    	  python27			pip2.7
     	  python36			pip3.6
          python38			pip3.8
        
# 下载第三方模块的句式:pip install 模块名
# 下载第三方模块临时切换仓库: pip install 模块名 -i 仓库地址
# 下载第三方模块指定版本(不指定默认是最新版): pip install 模块名==版本号 -i 仓库地址
    	
      	
"""
下载第三方模块可能会出现的问题
	1.报错并有警告信息
         WARNING: You are using pip version 20.2.1;
         原因在于pip版本过低 只需要拷贝后面的命令执行更新操作即可
         d:\python38\python.exe -m pip install --upgrade pip
         更新完成后再次执行下载第三方模块的命令即可
	2.报错并含有Timeout关键字
		说明当前计算机网络不稳定 只需要换网或者重新执行几次即可
	3.报错并没有关键字
		面向百度搜索
			pip下载XXX报错:拷贝错误信息
		通常都是需要用户提前准备好一些环境才可以顺利下载
	4.下载速度很慢
		pip默认下载的仓库地址是国外的 python.org
		我们可以切换下载的地址
		pip install 模块名 -i 仓库地址
		pip的仓库地址有很多 百度查询即可
		清华大学 :https://pypi.tuna.tsinghua.edu.cn/simple/
		阿里云:http://mirrors.aliyun.com/pypi/simple/
		中国科学技术大学 :http://pypi.mirrors.ustc.edu.cn/simple/
		华中科技大学:http://pypi.hustunique.com/
		豆瓣源:http://pypi.douban.com/simple/
		腾讯源:http://mirrors.cloud.tencent.com/pypi/simple
		华为镜像源:https://repo.huaweicloud.com/repository/pypi/simple/
"""
2.pycharm提供快捷方式

image

3.网络爬虫模块之request模块
requests模块能够模拟浏览器发送网络请求

import requests

res = requests.get('http://www.redbull.com.cn/about/branch')
print(res.content)  # 获取bytes类型的网页数据(二进制)
res.encoding = 'utf8'  # 指定编码
print(res.text)  # 获取字符串类型的网页数据(默认按照utf8)
4.网络爬虫实践之爬取链家二手房数据
import requests
import re

res = requests.get('https://sh.lianjia.com/ershoufang/pudong/')
data = res.text
home_title_list = re.findall(
    '<a class="" href=".*?" target="_blank" data-log_index=".*?"  data-el="ershoufang" data-housecode=".*?" data-is_focus="" data-sl="">(.*?)</a>',
    data)
home_name_list = re.findall('<a href=".*?" target="_blank" data-log_index=".*?" data-el="region">(.*?) </a>', data)
home_street_list = re.findall(
    '<div class="positionInfo"><span class="positionIcon"></span><a href=".*?" target="_blank" data-log_index=".*?" data-el="region">.*? </a>   -  <a href=".*?" target="_blank">(.*?)</a> </div>',
home_info_list = re.findall('<div class="houseInfo"><span class="houseIcon"></span>(.*?)</div>', data)
home_watch_list = re.findall('<div class="followInfo"><span class="starIcon"></span>(.*?)</div>', data)
home_total_price_list = re.findall(
    '<div class="totalPrice totalPrice2"><i> </i><span class="">(.*?)</span><i>万</i></div>', data)
home_unit_price_list = re.findall(
    '<div class="unitPrice" data-hid=".*?" data-rid=".*?" data-price=".*?"><span>(.*?)</span></div>', data)
home_data = zip(home_title_list, home_name_list, home_street_list, home_info_list, home_watch_list,
                home_total_price_list, home_unit_price_list)
with open(r'home_data.txt','w',encoding='utf8') as f:
    for data in home_data:
        print(
            """
            房屋标题:%s
            小区名称:%s
            街道名称:%s
            详细信息:%s
            关注程度:%s
            房屋总价:%s
            房屋单价:%s
            """%data
        )
        f.write("""
                房屋标题:%s
                小区名称:%s
                街道名称:%s
                详细信息:%s
                关注程度:%s
                房屋总价:%s
                房屋单价:%s\n
                """%data)
5.自动化办公领域openpyx
1.openpyxl操作 '''学会看官方文档!!!'''

    from openpyxl import Workbook
    # 创建一个excel文件
    wb = Workbook()
    # 在一个excel文件内创建多个工作簿
    wb1 = wb.create_sheet('学生名单')
    wb2 = wb.create_sheet('购物清单')
    wb3 = wb.create_sheet('VIP顾客名单')
    # 还可以修改默认的工作簿位置
    wb4 = wb.create_sheet('VIP顾客名单', 0)
    # 还可以二次修改工作簿名称
    wb4.title = 'VIP顾客名单'
    wb4.sheet_properties.tabColor = "1072BA"
    # 填写数据的方式
    wb4.append(['编号', '姓名', '年龄', '爱好'])  # 表头字段
    wb4.append([1, 'jason', 18, 'read'])
    wb4.append([2, 'kevin', 28, 'music'])
    wb4.append([3, 'tony', 58, 'play'])
    wb4.append([4, 'oscar', 38, 'ball'])
    wb4.append([5, 'jerry', 'ball'])
    wb4.append([6, 'tom', 88,'ball','哈哈哈'])
    # 保存该excel文件
    wb.save(r'111.xlsx')

"""
openpyxl主要用于数据的写入 至于后续的表单操作它并不是很擅长 
如果想做需要更高级的模块 pandasimport pandas

data_dict = {
    "公司名称": comp_title_list,
    "公司地址": comp_address_list,
    "公司邮编": comp_email_list,
    "公司电话": comp_phone_list
 }
# 将字典转换成pandas里面的DataFrame数据结构
df = pandas.DataFrame(data_dict)
# 直接保存成excel文件
df.to_excel(r'pd_comp_info.xlsx')



excel软件正常可以打开操作的数据集在10万左右 一旦数据集过大 软件操作几乎无效
需要使用代码操作>>>:pandas模块
"""

标签:comp,list,爬虫,办公,模块,自动化,pip,home,data
From: https://www.cnblogs.com/almira998/p/16830146.html

相关文章

  • 第三方模块下载与使用,爬虫requests模块,自动化办公领域openpyxl模块,pandas初见
    目录昨日内容回顾第三方模块下载与使用,爬虫requests模块,自动化办公领域openpyxl模块,pandas初见今日内容概要今日内容详细第三方模块的下载与使用网络爬虫模块之requests模......
  • python爬虫练习3
    说明python3.6.132位获取豆瓣电影推荐页电影详情,参考网址​​​https://movie.douban.com/explore#!type=movie&tag=%E7%83%AD%E9%97%A8&sort=recommend&page_limit=20&......
  • python爬虫练习2
    难度★目标网站​​http://quotes.toscrape.com/tag/humor/​​用到库scrapy1.4系统说明python3.6.164位目标获取名言与作者等(分页)新建文件quotes_spider.py,输入一下代码:i......
  • python爬虫练习1
    目录索引:​​python爬虫练习6:今日头条搜索图集​​​​python爬虫练习5:博客阅读量助手​​python爬虫练习4:刷浏览量​​python爬虫练习3:豆瓣电影推荐页电影详情​​​​pyth......
  • Python——爬虫准备工作
    Python——爬虫准备工作第三方模块的下载与使用'''第三方模块: 别人写的模块,一般情况下,功能都非常强大 使用第三方模块: 第一次使用必须先下载,后面才可以反复使用(下......
  • 爬虫、openpyxl、pandas
    目录今日内容概要今日内容详细作业详解第三方模块的下载与使用网络爬虫模块之requests模块网络爬虫实战之爬取链家二手房数据自动化办公领域之openpyxl模块homework今日内......
  • python进阶之路21 正则应用 第三方模块之requests模块 openpyxl模块 简易爬虫(panda
    作业讲解"""网络爬虫没有我们现在接触的那么简单 有时候页面数据无法直接拷贝获取 有时候页面还存在防爬机制弄不好ip会被短暂拉黑"""http://www.redbull.com.cn/ab......
  • day22爬虫
    第三方模块的下载与使用网络爬虫模块之requests模块网络爬虫实战之爬取链家二手房数据自动化办公领域之openpyxl模块今日内容详细作业讲解"""网络爬虫没有我们......
  • 网络爬虫之requests模块
    第三方模块的下载与使用网络爬虫模块之requests模块网络爬虫实战之爬取链接二手房数据自动化办公领域之openpyxl模块第三方模块的扩展(模块叠模块)网络爬虫之小实战......
  • requests模块/openpyxl模块/简单爬虫实战
    内容概要第三方模块的下载及使用网络爬虫及requests模块网络爬虫实战爬取二手房信息自动化办公领域模块openpyxl练习题及答案第三方模块的下载第三方模块就类似与......