day111 爬虫第一天
一、模拟浏览器发请求.
import requests
r1 =requests.get(
url ="https://dig.chouti.com/",
headers ={
"user-agent":'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36' # 模拟浏览器
}
)
print(r1.text)
二、拿到访问的cookie (cookie.get_dict)
import requests
r1 =requests.get(
url ="https://dig.chouti.com/",
headers ={
"user-agent":'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
}
)
r1_cookie_dict =r1.cookies.get_dict() #取cookie方式.
print(r1_cookie_dict)
三 、 通过拿到的Cookie自动登录
import requests
r1 =requests.get(
url ="https://dig.chouti.com/",
headers ={
"user-agent":'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
}
)
r1_cookie_dict =r1.cookies.get_dict()
print(r1_cookie_dict)
打印cookie 数据{'gpsd': '2b374387cb18e6231dad05778939ed9e', 'JSESSIONID': 'aaaq8zR3Ff_WQ8XSSeysw'}
import requests
r2 =requests.post(
url= 'https://dig.chouti.com/login',
headers={
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
}, # headers 里的数据为请求头.
data={
"phone":"8618611998441",
"password":"xxx",
"oneMonth":1
}, #data 里的数据为请求体.
cookies =r1_cookie_dict #通过第一次访问拿到cookie
)
print(r2.text) #打印请求结果 打印结果:{"result":{"code":"9999", "message":"", "data":{"complateReg":"0","destJid":"cdu_53188065757"}}}
四、点赞请求
r3 =requests.post(
url="https://dig.chouti.com/link/vote?linksId=20889331",
headers={
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
},
cookies =r1_cookie_dict
)
print( "r3.text===>",r3.text)
打印结果:r3.text===> {"result":{"code":"30010", "message":"你已经推荐过了", "data":""}}
总结 (三步骤)
#第一步 拿到cookie
import requests
r1 =requests.get(
url ="https://dig.chouti.com/",
headers ={
"user-agent":'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
}
)
r1_cookie_dict =r1.cookies.get_dict()
print("r1_cookie====>",r1_cookie_dict) #第二步登录
import requests
r2 =requests.post(
url= 'https://dig.chouti.com/login',
headers={
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
},
data={
"phone":"",
"password":"xxx",
"oneMonth":
},
cookies =r1_cookie_dict
)
print("r2.text===>",r2.text) #第三步点赞
r3 =requests.post(
url="https://dig.chouti.com/link/vote?linksId=20889331",
headers={
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
},
cookies =r1_cookie_dict
)
print( "r3.text===>",r3.text)
作业
作业:
1. 爬取抽屉新热榜的新闻:
标题
简介
地址
图片
2. 煎蛋网
- 爬取标题+简介
- 爬取图片
一 、 抽屉网站爬虫
import os
import requests
from bs4 import BeautifulSoup #1. 伪造浏览器发送请求
r1 =requests.get(
url = "https://www.autohome.com.cn/news/"
)
r1.encoding="gbk"
print(r1.text) #2.去响应 的响应体中解析我们想要的数据.
soup =BeautifulSoup(r1.text,"html.parser") #3. 找名字按照响应的规则:div 标签且 id = auto -channel-lazyload-article找匹配成功的第一个
container =soup.find(name="div",attrs={"id":"auto-channel-lazyload-article"}) #4.去container中找所有的li标签
li_list =container.find_all(name ="li")
for tag in li_list:
title =tag.find(name ="h3")
if not title:
continue
summary =tag.find(name="p")
a =tag.find(name="a")
url ="https:"+a.attrs.get("href") img= tag.find(name="img")
img_url= "https:"+img.get("src")
print(title.text)
print(summary.text)
print(url)
print((img_url)) #下载图片
r2 =requests.get(
url=img_url
)
file_name =img_url.rsplit("/",maxsplit=1)[1]
file_path=os.path.join("imgs",file_name)
with open(file_path,"wb")as f:
f.write(r2.content)
"""
作业:
1. 爬取抽屉新热榜的新闻:
标题
简介
地址
图片
2. 煎蛋网
- 爬取标题+简介
- 爬取图片 """
import requests
from bs4 import BeautifulSoup #1. 伪造浏览器发送请求
r1 =requests.get(
url="https://dig.chouti.com",
headers={
"user-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36"
}
) #2.去响应的响应体重解析我们想要的数据
soup =BeautifulSoup(r1.text,"html.parser")
container = soup.find(name ="div",attrs={"id":"content-list"})
div_list = container.find_all(name ="div",attrs = {"class":"part1"}) # 1 拿到标题
# for item in div_list:
# title = item.find(name ="a")
# title =title.text
# title =title.strip()
# print(title) #2 拿到简介 # div_list = container.find_all(name ="div",attrs = {"class":"area-summary"})
# for item in div_list:
# summary = item.find(name ="span",attrs ={"class":"summary"})
# print(summary,type(summary)) #3.拿到地址: # for item in div_list:
# tag =item.find(name ="a",attrs = {"class":"show-content color-chag"})
# url=tag.attrs.get("href")
# print(url) #4. 图片. div_item =container.find_all(name ="div",attrs ={"class":"item"})
for item in div_item:
div_pic = item.find(name="div", attrs={"class": "news-pic"})
print(div_pic)
pic =div_pic.find("img")
img_url ="https://"+pic.get("original") #图片的url
print(img_url)
二 、煎蛋网爬虫
import requests
from bs4 import BeautifulSoup r1 =requests.get(
url ="http://jandan.net"
)
soup =BeautifulSoup(r1.text,"html.parser")
container = soup.find(name ="div",attrs={"id":"content"})
div_list = container.find_all( name ="div",attrs={"class": "post f list-post"}) #1 打印出所有的标题.
# for item in div_list:
# div_index =item.find(name ="div",attrs ={"class":"indexs"})
# title = div_index.find(name ="h2")
# title =title.find(name="a")
# print(title.text) #2 .打印出所有的简介. for item in div_list:
div_index =item.find(name ="div",attrs ={"class":"indexs"})
# print(len(div_index.contents))
print(div_index.contents[6])#共计7个长度,标签之间空格也算一个。
day111 爬虫第一天的更多相关文章
- python爬虫第一天
python爬虫第一天 太久没折腾爬虫 又要重头开始了....感谢虫师大牛的文章. 接下来的是我的随笔 0x01 获取整个页面 我要爬的是百度贴吧的图,当然也是跟着虫师大牛的思路. 代码如下: #co ...
- Python爬虫第一步
这只是记录一下自己学习爬虫的过程,可能少了些章法.我使用过的是Python3.x版本,IDE为Pycharm. 这里贴出代码集合,这一份代码也是以防自己以后忘记了什么,方便查阅. import req ...
- 爬虫第一篇基本库的使用——urllib
在Python2中有urllib2和urllib3两个库来实现请求的发送,在Pyhon3中则统一为urllib. urilib包含以下4个模块 request:最基本的请求模块,可以用来实现请求的发送 ...
- 【Python3爬虫】学习分布式爬虫第一步--Redis分布式爬虫初体验
一.写在前面 之前写的爬虫都是单机爬虫,还没有尝试过分布式爬虫,这次就是一个分布式爬虫的初体验.所谓分布式爬虫,就是要用多台电脑同时爬取数据,相比于单机爬虫,分布式爬虫的爬取速度更快,也能更好地应对I ...
- 03.Python网络爬虫第一弹《Python网络爬虫相关基础概念》
爬虫介绍 引入 之前在授课过程中,好多同学都问过我这样的一个问题:为什么要学习爬虫,学习爬虫能够为我们以后的发展带来那些好处?其实学习爬虫的原因和为我们以后发展带来的好处都是显而易见的,无论是从实际的 ...
- Python网络爬虫第一弹《Python网络爬虫相关基础概念》
爬虫介绍 引入 之前在授课过程中,好多同学都问过我这样的一个问题:为什么要学习爬虫,学习爬虫能够为我们以后的发展带来那些好处?其实学习爬虫的原因和为我们以后发展带来的好处都是显而易见的,无论是从实际的 ...
- Python爬虫第一个成功版
爬取http://www.mzitu.com/all里面的图片 import urllib.request import re import os url = 'http://www.mzitu.co ...
- 03,Python网络爬虫第一弹《Python网络爬虫相关基础概念》
爬虫介绍 引入 为什么要学习爬虫,学习爬虫能够为我们以后的发展带来那些好处?其实学习爬虫的原因和为我们以后发展带来的好处都是显而易见的,无论是从实际的应用还是从就业上. 我们都知道,当前我们所处的时代 ...
- 爬虫第一篇:爬虫详解之urllib.request模块
我将urllib.request 的GET请求和POST请求两种方法做了总结 GET请求 GET请求爬取: import urllib.request import urllib.parse head ...
随机推荐
- php.ini memory_limit引起的问题
故障现象 在运行PHP程序,通常会遇到“Fatal Error: Allowed memory size of xxxxxx bytes exhausted”的错误, 这个意味着PHP脚本使用了 ...
- php多进程 防止出现僵尸进程
对于用PHP进行多进程并发编程,不可避免要遇到僵尸进程的问题. 僵尸进程是指的父进程已经退出,而该进程dead之后没有进程接受,就成为僵尸进程(zombie)进程.任何进程在退出前(使用exit退出) ...
- 面向对象设计模式纵横谈:Bridge 桥接模式(笔记记录)
桥接模式是一个比较难理解的设计模式,设计和分析的时候也不容易把握,咱们听听“李建忠”老师是怎么来讲的.我们还是从演变的角度来说问题,一步一步的来把问题说清楚.先谈谈“抽象”和“实现”的关系. 抽象与实 ...
- Rakefile实例教程
一.简介 简单的说Rakefile就是使用Ruby语法的makefile, 对应make的工具就是rake. 在Ruby on Rails里面, 不管是数据库的初始化, 内容初始化, 删除, 还是测试 ...
- Js下载文件到本地(兼容多浏览器)
在客户端通过js下载文件,试过几种下载方式,iframe方式仅限于IE浏览器,window.open(url),location.href=url 这两种方式在chrome浏览器还会是直接打开文件而不 ...
- jquery阻止表单提交
<form action="" method="post" onSubmit="return confirm();" > < ...
- Debian 使用 cron 执行定时任务
在linux下有两种方法来让一个命令或者脚本执行: crontab : 执行一个任务一次或者多次. at : 只执行一次. crontab是通过读取一个crontab文件来工作,这是一个普通的文本文件 ...
- 打开jsp页面时,显示空白页。
打开jsp页面时,显示空白页. #foreach($e in $listPlanItem) #set($listPlanDetail=$!e.get(2)) < ...
- SpringBoot集成篇(二) 异步调用Async
什么是异步调用? 异步调用是相对于同步调用而言的,同步调用是指程序按预定顺序一步步执行,每一步必须等到上一步执行完后才能执行,异步调用则无需等待上一步程序执行完即可执行. 如何实现异步调用? 多线程, ...
- 2018.09.30 bzoj3551:Peaks加强版(dfs序+主席树+倍增+kruskal重构树)
传送门 一道考察比较全面的题. 这道题又用到了熟悉的kruskal+倍增来查找询问区间的方法. 查到询问的子树之后就可以用dfs序+主席树统计答案了. 代码: #include<bits/std ...