zoukankan      html  css  js  c++  java
  • 爬虫 第二次入坑 基本流程

    1.爬虫的基本流程

    发送请求---》获取相应内容---》解析内容---》保存数据

     1 #1、发起请求
     2 使用http库向目标站点发起请求,即发送一个Request
     3 Request包含:请求头、请求体等
     4 
     5 #2、获取响应内容
     6 如果服务器能正常响应,则会得到一个Response
     7 Response包含:html,json,图片,视频等
     8 
     9 #3、解析内容
    10 解析html数据:正则表达式,第三方解析库如Beautifulsoup,pyquery等
    11 解析json数据:json模块
    12 解析二进制数据:以b的方式写入文件
    13 
    14 #4、保存数据
    15 数据库
    16
    1 #http协议:http://www.cnblogs.com/haiyan123/p/7298967.html
    2 
    3 #Request:用户将自己的信息通过浏览器(socket client)发送给服务器(socket server)
    4 
    5 #Response:服务器接收请求,分析用户发来的请求信息,然后返回数据(返回的数据中可能包含其他链接,如:图片,js,css等)
    6 
    7 #ps:浏览器在接收Response后,会解析其内容来显示给用户,而爬虫程序在模拟浏览器发送请求然后接收Response后,是要提取其中的有用数据。

    Request

    1和2只是在response处有所不同

    1是先把url后面的分组处理了下,然后字符串拼接成url,2是直接把分组的值放到params这个字典里。其它地方都一样。结果也一样。

     1 import requests
     2 from urllib.parse import urlencode
     3 
     4 kwords = input('please input keywords:>> ').strip()
     5 res = urlencode({'wd': kwords})
     6 print(res)  # wd=%E5%B0%8F%E7%8B%97
     7 url = 'http://www.baidu.com/s?' + res
     8 print(url)  # http://www.baidu.com/s?wd=%E5%B0%8F%E7%8B%97
     9 
    10 response = requests.get(
    11     url,
    12     headers={
    13         # google的User-Agent用above:version查找
    14         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
    15 
    16     },
    17 
    18 )
    19 with open('a.html','w',encoding = 'utf-8') as f:
    20     # f.write(response.text)把获得的响应体写入到文件中
    21     print(f.write(response.text))
    22 
    23 print(response.status_code)
    示例一
     1 kwords = input("请输入关键字:>>").strip()
     2 response = requests.get(
     3     "https://www.baidu.com/s?",
     4     # 请求的url,当你在百度输入中文的时候,你把url拿下来会变成下面的这样格式的url
     5     params={
     6         "wd":kwords,
     7         'pn':20
     8     },
     9     # 请求头
    10     headers={
    11         "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36",
    12     },
    13 )
    14 with open("b.html","w",encoding="utf-8") as f:
    15     f.write(response.text)
    16 # print(response.status_code)
    17 
    18 示例代码二(和上面的结果是一样的)
    示例二

    Response

     1 #1、响应状态
     2     200:代表成功
     3     301:代表跳转
     4     404:文件不存在
     5     403:权限
     6     502:服务器错误
     7 
     8 #2、Respone header
     9     Location:跳转
    10     set-cookie:可能有多个,是来告诉浏览器,把cookie保存下来
    11     
    12 #3、preview就是网页源代码
    13     最主要的部分,包含了请求资源的内容
    14     如网页html,图片
    15     二进制数据等

    总结

     1 #1、总结爬虫流程:
     2     爬取--->解析--->存储
     3 
     4 #2、爬虫所需工具:
     5     请求库:requests,selenium
     6     解析库:正则,beautifulsoup,pyquery
     7     存储库:文件,MySQL,Mongodb,Redis
     8 
     9 #3、爬虫常用框架:
    10     scrapy
  • 相关阅读:
    Linked List Cycle leetcode java (链表检测环)
    Remove Duplicates from Sorted List II leetcode java
    Remove Duplicates from Sorted List leetcode java
    Merge Two Sorted Lists leetcode java
    Swap Nodes in Pairs leetcode java
    Median of Two Sorted Array leetcode java
    阿里云最便宜的四种域名注册
    nohup和&后台运行,进程查看及终止
    ipv6转ipv4 NAT64与DNS64基本原理概述
    ros使用pppoe拨号获取ipv6,并且下发IPV6的dns到客户机win7
  • 原文地址:https://www.cnblogs.com/Roc-Atlantis/p/9588204.html
Copyright © 2011-2022 走看看