python爬虫简介
一、什么是网络爬虫?
网络爬虫,是一种按照一定规则,自动的抓取万维网信息的程序或者脚本。
二、python网络爬虫,
需要用到的第三方包 requests和BeautifulSoup4
pip install requests
pip install BeautifulSoup4
常用方法总结:
response = requests.get('URL') #获取网
response.text #文本内容(字符串
response.content #文件内容,比如图
response.encoding #设置编
response.aperant_encoding #显示下载时候的编
response.status_code #状态码
response.cookies.get_dict()
requests.get('http://www.autohome.com.cn/news/',cookie={'xx':'xxx'})
beautifulsoup4模块
soup = BeautifulSoup('htmlstr',features='html.parser')
v1 = soup.find('div')
v1 = soup.find(id = 'i1')
v1 = soup.find('div',id = 'i1') v2 = soup.find_all('div')
v2 = soup.find_all(id = 'i1')
v2 = soup.find_all('div',id = 'i1')
v1.text #字符串
v1.attr #属性
#v2是个列表
v2[0].attr
三、初始demo
import requests
from bs4 import BeautifulSoup
response = requests.get(url = 'https://www.autohome.com.cn/news/') #下载页面
response.encoding = response.apparent_encoding
soup = BeautifulSoup(response.text,features='html.parser') #创建Beautisoup对象
target = soup.find(id='auto-channel-lazyload-article') #找到新闻栏
#print(target)
li_list = target.find_all('li')
for i in li_list:
a = i.find('a')
if a:
print(a.attrs.get('href'))
txt = a.find('h3').text
imagurl = a.find('img').attrs.get('src')
print(imagurl) img_response = requests.get(url = 'https:'+imagurl)
import uuid
file_name = str(uuid.uuid4())+'.jpg'
with open(file_name,"wb") as f:
f.write(img_response.content)
四、抽屉登录并点赞
'''
抽屉小套路,用户认证的cookie不是登录用户密码返回的cookie
而是第一次get返回的cookie,然后登陆的时候把这个cookie带过去进行授权操作
'''
import requests headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
}
post_data = {
'phone':'8615191481351',
'password':'11111111',
'oneMonth':1
}
ret1 = requests.get(
url = 'https://dig.chouti.com',
headers = headers
)
cookie1 = ret1.cookies.get_dict()
print(cookie1) ret2 = requests.post(
url = 'https://dig.chouti.com/login',
data = post_data,
headers = headers,
cookies = cookie1
)
cookie2 = ret2.cookies.get_dict()
print(cookie2) ret3 = requests.post(
url = 'https://dig.chouti.com/link/vote?linksId=21910661',
cookies = {
'gpsd':cookie1['gpsd']
#'gpsd': 'f59363bb59b30fe7126b38756c6e5680'
},
headers = headers
)
print(ret3.text) ret = requests.post(
url = 'https://dig.chouti.com/vote/cancel/vote.do',
cookies = {
'gpsd': cookie1['gpsd']
},
data = {'linksId': 21910661},
headers = headers
)
print(ret.text)
更多关于request参数的介绍:http://www.cnblogs.com/wupeiqi/articles/6283017.html
python爬虫简介的更多相关文章
- python 爬虫简介
初识Python爬虫 互联网 简单来说互联网是由一个个站点和网络设备组成的大网,我们通过浏览器访问站点,站点把HTML.JS.CSS代码返回给浏览器,这些代码经过浏览器解析.渲染,将丰富多彩的网页呈现 ...
- python 爬虫简介以及使用方法
阶段大纲: 一. 爬虫 1. 基本操作 - 登录任意网站(伪造浏览器的任何行为) 2. 性能相关 - 并发方案: - 异步IO: gevent/Twisted/asyncio/aiohttp - 自定 ...
- Python爬虫入门
Python爬虫简介(来源于维基百科): 网络爬虫始于一张被称作种子的统一资源地址(URLs)列表.当网络爬虫访问这些统一资源定位器时,它们会甄别出页面上所有的超链接,并将它们写入一张"待访列表",即 ...
- Python爬虫教程-01-爬虫介绍
Spider-01-爬虫介绍 Python 爬虫的知识量不是特别大,但是需要不停和网页打交道,每个网页情况都有所差异,所以对应变能力有些要求 爬虫准备工作 参考资料 精通Python爬虫框架Scrap ...
- Python爬虫教程-04-response简介
Spider-04-response简介 本小节介绍urlopen的返回对象,和简单调试方法 案例v3 研究request的返回值,输出返回值类型,打印内容 geturl:返回请求对象的url inf ...
- Python爬虫教程-20-xml 简介
本篇简单介绍 xml 在python爬虫方面的使用,想要具体学习 xml 可以到 w3school 查看 xml 文档 xml 文档链接:http://www.w3school.com.cn/xmld ...
- Python 网络爬虫 001 (科普) 网络爬虫简介
Python 网络爬虫 001 (科普) 网络爬虫简介 1. 网络爬虫是干什么的 我举几个生活中的例子: 例子一: 我平时会将 学到的知识 和 积累的经验 写成博客发送到CSDN博客网站上,那么对于我 ...
- Python爬虫和情感分析简介
摘要 这篇短文的目的是分享我这几天里从头开始学习Python爬虫技术的经验,并展示对爬取的文本进行情感分析(文本分类)的一些挖掘结果. 不同于其他专注爬虫技术的介绍,这里首先阐述爬取网络数据动机,接着 ...
- Python爬虫教程-21-xpath 简介
本篇简单介绍 xpath 在python爬虫方面的使用,想要具体学习 xpath 可以到 w3school 查看 xpath 文档 xpath文档:http://www.w3school.com.cn ...
随机推荐
- Linux编程之文件锁
1. 使用 fcntl() 给记录加锁 使用 fcntl() 能够在一个文件的任意部分上放置一把锁,这个文件部分既可以是一个字节,也可以是整个文件.这种形式的文件加锁通常被称为记录加锁,但这种称谓是不 ...
- Copy-On-Write in Swift
Premature optimisation is the root of all evil. But, there are moments where we need to optimise our ...
- LC 539. Minimum Time Difference
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minut ...
- 【经验】PHP开发中  导致页头一行空白
PHHP开发中 有的时候遇到页面顶部多出一行空白,审查元素发现头部有一行 比如在$this->dispay();方法中最前面加入ob_clean(); ================ ...
- SQL SERVER 数据库安装完毕之后如何修改数据库实例排序规则
背景 最近我们在azure portal 上开了几台英文版的数据库服务器,因默认是开启就安装好对应的数据库,所以存在一个实例排序规则的问题,需把整个实例都调整成Chinese_PRC_CI_AS,避免 ...
- 爬取网贷之家平台数据保存到mysql数据库
# coding utf-8 import requests import json import datetime import pymysql user_agent = 'User-Agent: ...
- Servlet(4):Session
Session, Cookie及交互 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session. Cookie通过在客户端记录信息确 ...
- 使用PhantomJS
PhantomJS是一个基于WebKit的服务器端JavaScript API.它全面支持Web而无需浏览器支持,不仅运行快,原生支持各种web标准:DOM处理.CSS选择器.JSON.Canvas, ...
- Qt qss问题总结
1.在QWidget中设定了setObjectName,就是不起作用. 解决方法重写paintEvent. #ifndef BROWSEWIDGET_H #define BROWSEWIDGET_H ...
- redis安装及遇到的坑-linux
获取Redis安装包“redis-4.0.8.tar.gz”,上传Linux服务器; 使用root用户解压: tar zxvf redis-4.0.8.tar.gz -C /usr/local/; 进 ...