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. 登录日志 cat /var/log/secure 涉及到账号登录的日志信息都会记录在此文件中. 2. Unit 的启动日志 journalctl 可以查看所有 unit 的启动日志,日志的配置文 ...
- IDEA无法通过类加载器获取resources文件夹配置文件解决办法
问题描述:如果IDEA无法通过类加载器获取resources文件夹配置文件,一定是Classpath编译文件没有导致的. 1.在通过配置文件来获取文件信息时,在resouces文件中放入了filena ...
- android canvas drawtext 字高
Paint pFont = new Paint(); Rect rect = new Rect(); pFont.getTextBounds("豆", 0, 1, rect); L ...
- leetcode-hard-array-287. Find the Duplicate Number
mycode 77.79% class Solution(object): def findDuplicate(self, nums): """ :type nums ...
- EXCEL中自定义格式输入的数据怎么完整复制
在用设置单元格式里 自定义 输入数值 如图,B列的数据,我复制后,用选择性粘贴到别的地方,还是无法将75FG4Y2一起复制过去,只能复制过去FG 怎么办? ===>先把这些复制到一个记事本里,再 ...
- Dialog 对话框
在保留当前页面状态的情况下,告知用户并承载相关操作. 基本用法 Dialog 弹出一个对话框,适合需要定制性更大的场景. 需要设置visible属性,它接收Boolean,当为true时显示 Dial ...
- 【转】java导出多个excel表格,并压缩成zip输出
转自:http://blog.csdn.net/qq_14861089/article/details/53169414 感谢作者分享 /** * 导出支付宝批量支付文件excel * * @p ...
- java源码-ReentrantLock源码分析-1
ReentrantLock 继承于lock是比较常用的独占锁,接下来我们来分析一下ReentrantLock源码以及接口设计: Sync是ReentrantLock的内部静态抽象类继承Abstract ...
- SQL Server 等待统计信息基线收集
背景 我们随时监控每个服务器不同时间段的wait statistics ,可以根据监控信息大概判断什么时候开始出现异常,相当于一个wait statistics基线收集,还可以具体分析占比高的等待类型 ...
- Kafka集群安裝部署(自带Zookeeper)
kafka简介 kafka官网:http://kafka.apache.org/ kafka下载页面:http://kafka.apache.org/downloads kafka配置快速入门:htt ...