• 爬虫


    爬虫

    • 基本操作

    • 多进程、线程、协程

    • 高性能相关(socket-select)

      • twisted
      • tornado
      • gevent
    • web版微信(练习)

    • Scrapy框架(爬虫框架)规则

    • 自己的爬虫框架

    1.基本操作

    定向爬取(只爬一个网站)和广泛的爬取(通过友情链接)

    爬取某url中的指定内容:

    • 发送http请求:http//www.xxx.com/news/
    • 通过正则表达式获取内容

    python实现以上流程:

    • 利用requests发请求
    • 方法:
      • obj = requests.get(url) 发送post请求
      • obj.content # 获取字节
      • obj.encoding = 'gbk' # 指定字符编码
      • obj.text # 获取文本
    import requests
    response = requests.get('http://....')
    # response.content # 获取字节
    response.encoding = 'gbk' # 指定字符编码
    response.text # 获取文本		
    
    • 正则匹配
    • 方法:
      • soup = BeautifulSoup(obj.text,'html.parser') # python内置标签对象解析器:html.parser'
      • find
        • tag_obj= soup.find('a') # 通过标签
        • tag_obj= soup.find(id='i1') # 通过属性
        • tag_obj.find('p') # 标签对象可以再find
        • tag_obj.find(class_='c1') # class属于关键字可以使用class_代替
      • find_all
        • [tag_obj,...] = soup.find_all('a') # 找到所有标签
        • [tag_obj,...] = soup.find_all(attrs={'class':'xxxx'}) # 找到所有class=xxxx的标签
      • 获取属性
        • tag_obj.text # 标签文本内容
        • tag_obj.attrs # 获取标签属性字典,这个标签的所有属性,也可以li.find('a').attrs
        • tag_obj.get('href') # 获取一个属性,也可以li.find('a').attrs['href']url = li.find('a').get('href')
    from bs4 import beautifulsoup	
    
    soup = BeautifulSoup(response.text,'html.parser') # python内置标签对象解析器:html.parser'
    
    # 一、获取一个标签
    tag = soup.find(id='ssss',class_='c1') 
    # tag = soup.find(id='ssss',attrs={'class':'c1','name':'h3'})
    h3 = tag.find(name='h3') # 标签名为h3
    
    # 二、获取批量标签
    li_list = soup.find(id='ssss').find_all(name='li') # find和find_all可以互相跟随
    for li in li_list:
    
    	# 文章标题
    	title = li.find('h3') # 默认是name
    	if not title:
    		continue
    	print(title.text) # 标签文本内容
    	
    	# 文章内容
    	summary = li.fund('p').text
    	
    	# 下载图片
    	img = li.find('img').get('src') # 获取图片url
    	res = request.get(img)
    	file_name = '%s.jpg'% title
    	with open(file_name,'wb') as f:
    		f.write(res.content)
    
    • ps:

    pip3 install requests
    pip3 install BeautifulSoup4

    1.1python 代码登录github

    • 模式1:
      • 1,get请求登录页面
      • 2,获取csrftoken
      • 3,带用户名、密码、scrftoken,发送post请求
      • 4,获取cookie
      • 5,携带cookies发送其他请求
    r1 = soup.find(name='input', attrs={'name': "csrf_token"}).get('value')
    
    r2 = requests.post(url,data={'utf8':'✓','token':token,'login':username,'password':password,'commit':'Sign in'})
    
    r2 = requests.post(
        url,
        data={
            'commit': 'Sign in',
            'utf8': '✓',
            'authenticity_token': token,
            'login': username,
            'password': password,
        },
        cookies=cookie
    )
    
    r2.cookies # cookies获取
    cookies_dict = `r2.cookies.get_dict()` # 字典类型cookies
    
    r3 = `request.get(usl,cookies=cookies_dict)` # 携带cookies发送其他请求
    
    
    • 模式2:
      • 1,get请求登录页面
      • 2,获取csrftoken和第一次cookies
      • 3,带用户名、密码、scrftoken和第一次cookies,发送post请求
      • 4,获取第二次cookies
      • 5,整合第一次cookies和第二次cookies发送其他请求
    import requests
    from bs4 import BeautifulSoup
    
    # 1,get请求登录页面
    response = requests.get('https://github.com/login')
    
    # 2,获取csrftoken和第一次cookies
    cookie_1 = response.cookies.get_dict()
    soup = BeautifulSoup(response.text, 'html.parser')
    token_1 = soup.find(name='input', attrs={'name': "authenticity_token"}).get('value')
    
    # 3,带用户名、密码、scrftoken,发送post请求
    response_2 = requests.post(
        'https://github.com/session',
        data={
            'commit': 'Sign in',
            'utf8': '✓',
            'authenticity_token': token_1,
            'login': username,
            'password': password,
        },
        cookies=cookie_1
    )
    
    # 4, 获取第二次cookies
    cookie_2 = response_2.cookies.get_dict()
    
    # 5, 整合第一次cookies和第二次cookies发送其他请求
    cookie_dict = {}
    cookie_dict.update(cookie_1)
    cookie_dict.update(cookie_2)
    
    response_3 = requests.get('https://github.com/settings/emails', cookies=cookie_dict)
    soup_2 = BeautifulSoup(response_3.text,'html.parser')
    tag_text = soup_2.find(id='settings-emails').find('span').text
    print(tag_text)
    

    1.2 抽屉点赞

    1,登录
    2,标签

  • 相关阅读:
    unreal-python-howtos
    vscode plugin development
    [uva] 1671 History of Languages
    [codeup] 1128 出租车费
    [codeup] 1126 看电视
    Ubuntu 16.04 + ROS Kinetic 机器人操作系统学习镜像分享与使用安装说明
    (二)ROS系统架构及概念 ROS Architecture and Concepts 以Kinetic为主更新 附课件PPT
    ROS新功能包PlotJuggler绘图
    Winform DevExpress控件库(三) 使用NavBarControl控件定制导航栏
    数据意识崛起,从企业应用看BI软件的未来发展
  • 原文地址:https://www.cnblogs.com/sunqim16/p/7445504.html
Copyright © 2020-2023  润新知