爬虫笔记第一天

爬虫基础:

1.了解域名解析

DNS:域名解析服务器

访问百度时经过几个步骤:

首先本地host文件,接着经过DNS域名解析服务器 本地路由,百度服务器,请求的页面。

顶级域名跟二级域名的区别:

  顶级域名:也叫一级域名,根域名

  除了后缀以外只有一个点号

baidu.com 顶级域名

www.baidu.com 是一个二级域名

 2.什么是爬虫?

爬虫,可以理解为在网络上爬行的一只蜘蛛,互联网可看作是一张大网,爬虫就在这张网上爬取想要的资源。

爬虫本质上就是模仿浏览器对目标网站发送请求并获取到有用的数据。

一个http请求包含有请求头,请求体等。

当向目标网站发送请求并获取到返回的response之后,对response进行解析

response包含有响应状态,响应头以及响应体部分,响应体是我们需要解析的东西,包括网页HTML代码,图片等。

3.解析方式有哪些?

json,bs4,PyQuery,Xpath等

以下是爬取网站示例代码:

1.爬取汽车之家

# -*- coding: utf-8 -*-

import requests
#1.下载页面

ret = requests.get(url='https://www.autohome.com.cn/news/')
# print ret.apparent_encoding  #查看是什么编码
# ret.encoding = 'gbk'  #解决乱码问题或者写成
ret.encoding = ret.apparent_encoding
# print ret.text
#2.解析网页  获取想要的内容
from bs4 import BeautifulSoup
soup = BeautifulSoup(ret.text,'html.parser')
# print type(soup)
div = soup.find(name='div',id='auto-channel-lazyload-article')
li_list = div.find_all(name='li')
for li in li_list:
    h3 = li.find(name='h3')
    if not h3:
        continue
    print h3.text

    p = li.find(name='p')
    print p.text

    a = li.find('a')
    print a.get('href')  #获取a标签的属性a.attr

    img = li.find('img')
    src = img.get('src')
    print src
    file_name = src.rsplit('__',1)[1]
    print file_name
    ret_img = requests.get(url='https:'+ src)
    print ret_img
    # with open(file_name,'wb') as f:
    #     f.write(ret_img.content)

2.爬取抽屉网站

# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import requests
'''
抽屉网站的cookie分为登陆之前的cookie跟登陆后的cookie,当没登陆之前抽屉会给用户返回一个cookie
此时的cookie是没有授权的,因此不能用次cookie对网站做点赞等操作,只有在登陆之后获取的cookie才是授权过的,
可对网站进行点赞等操作
'''

url='https://dig.chouti.com/all/hot/recent/1'

res = requests.get(url,
                   headers={
                       'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
                   }
                   )
r1_cookie_dict = res.cookies.get_dict()  #获取未授权的cookie

print r1_cookie_dict
print res.status_code

#发送post请求进行登陆
res2 = requests.post(url='https://dig.chouti.com/login',
                    data= {
                            'oneMonth':'1',
                            'password':'ymm121286',
                            'phone':'8613761309761',
                    },
                    headers={
                       'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
                   },
                     cookies = r1_cookie_dict
                    )
cookie_dict = res2.cookies.get_dict()
# print cookie_dict  #获取授权后的cookie
print 'denglu',res2.status_code

#对文章进行点赞,发送post请求
# url_gd = 'https://dig.chouti.com/link/vote?linksId=20324196'
# res3 = requests.post(
#                         url = 'https://dig.chouti.com/link/vote?linksId=20324196',
#                         headers = {
#                             'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
#                         },
#                         # cookies = {'gpsd':'abf857c9b96fa7db262a5fc923da0181',
#                         #             'gpid':'bfb20936a04c4a42a303ee7f0a03c9c1'
# # }
#                         cookies = r1_cookie_dict
#     )
# print res3.text


#对页面所有文章进行点赞
#1.获取文章id
# print res.text
soup = BeautifulSoup(res.text,'html.parser')
div = soup.find(attrs={'class': 'content-list','id': 'content-list'})
for items in div.find_all(attrs={'class':'item'}):
    item = items.find(attrs={'class':'part2'})
    linkid = item.get('share-linkid')

    url_gd = 'https://dig.chouti.com/link/vote?linksId=%s'% linkid
    res3 = requests.post(
                            url = url_gd,
                            headers = {
                                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
                            },
                            # cookies = {'gpsd':'abf857c9b96fa7db262a5fc923da0181',
                            #             'gpid':'bfb20936a04c4a42a303ee7f0a03c9c1'
    # }
                            cookies = r1_cookie_dict
        )
    print res3.text

#如果需要对多页进行点赞
for page in range(2,3):
    url = 'https://dig.chouti.com/all/hot/recent/%s'%page
    response_index = requests.get(url,
                                  headers = {},
                                  )
    '....此处跟上面的点赞一样即可'



 作业:根据爬取抽屉网站对github进行自动登录并爬取个人信息

(提示:cookie很重要,分为授权以前的cookie,以及授权之后的cookie,可参考抽屉)

猜你喜欢

转载自www.cnblogs.com/ymb2580/p/9269591.html