网络爬虫(又被称为网页蜘蛛,网络机器人)就是模拟客户端发送网络请求,接收请求响应,一种按照一定的规则,自动地抓取互联网信息的程序。
只要是浏览器能做的事情,原则上,爬虫都能够做
先来看一下最简单的网络爬虫百度Logo图片提取:
import requests
r = requests.get("https://www.baidu.com/img/bd_logo1.png")
with open("baidu.png","wb") as f:
f.write(r.content)
接下来按照爬虫基本工作流程提取内涵社区网站文本内容:
1.获取url:
url=http://neihanshequ.com/
headers= {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"}
2.发送请求,接受响应:
r = requests.get(url,headers=headers)
html_str = r.content.decode()
3.提取数据:
'''
公交车上,一小伙对着手机打公放点微信语音:“臭小子,你是不是一点也不想你老娘?都不知道陪我聊聊天,回家来看看我...”每点开这段语音,小伙都自言自语说道:“别唠叨啦,我每天都陪你聊天,好不好!想你了,老妈...”一大爷看不下去了,说道:“小伙子!你能不能不循环播这段语音,你要是想你妈了,能不能给她打个电话?”小伙说道:“上个月她走了,就只剩下这段语音了...”
'''
t = re.findall(r".*?
(.*?)
.*?",html_str,re.S)
4.保存数据:
with open("neihan.txt","w",encoding="utf-8") as f:
for i in t:
f.write(i)
f.write("\n")
import re
import requests
def Neihan():
url=http://neihanshequ.com/
headers= {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"}
r = requests.get(url,headers=headers)
html_str = r.content.decode()
'''
公交车上,一小伙对着手机打公放点微信语音:“臭小子,你是不是一点也不想你老娘?都不知道陪我聊聊天,回家来看看我...”每点开这段语音,小伙都自言自语说道:“别唠叨啦,我每天都陪你聊天,好不好!想你了,老妈...”一大爷看不下去了,说道:“小伙子!你能不能不循环播这段语音,你要是想你妈了,能不能给她打个电话?”小伙说道:“上个月她走了,就只剩下这段语音了...”
'''
t = re.findall(r".*?
(.*?)
.*?",html_str,re.S)
with open("neihan.txt","w",encoding="utf-8") as f:
for i in t:
f.write(i)
f.write("\n")
Neihan()
按照面向对象爬取内涵社区网站文本,爬虫工作流程代码如下:
# coding=utf-8
import requests
import re
import json
class Neihan:
def __init__(self):
self.start_url = "http://neihanshequ.com/"
self.headers = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"}
self.next_url_temp = "http://neihanshequ.com/joke/?is_json=1&app_name=neihanshequ_web&max_time={}"
def parse_url(self,url): #发送url地址的请求,获取响应
r = requests.get(url,headers=self.headers)
return r.content.decode()
def get_first_page_content_list(self,html_str):
t = re.findall(r".*?
(.*?)
.*?", html_str, re.S)
#获取max——time
max_time = re.findall("max_time: '(.*?)'",html_str,re.S)[0]
return t,max_time
def save_content_list(self,content_list): #保存
for content in content_list:
print(content)
def get_content_list(self,html_str):
dict_response = json.loads(html_str)
content_list = [i["group"]['text'] for i in dict_response["data"]["data"]]
max_time = dict_response["data"]["max_time"]
#获取has_more
has_more = dict_response["data"]["has_more"]
return content_list,max_time,has_more
def run(self):#实现主要逻辑
#1.start_url
#2.发送请求,获取响应
html_str = self.parse_url(self.start_url)
#3.提取数据
content_list,max_time = self.get_first_page_content_list(html_str)
#4.保存
self.save_content_list(content_list)
#5.获取第二页的url
has_more=True
while has_more:
next_url = self.next_url_temp.format(max_time)
html_str = self.parse_url(next_url) #发送下一页的请求
content_list,max_time,has_more = self.get_content_list(html_str)#获取json中的段子和max——time
self.save_content_list(content_list)
if __name__ == '__main__':
neihan = Neihan()
neihan.run()