Teach you how to write a simple first reptiles

Many people know reptiles, reptile climb also want to use the data they want to take, then the reptiles in the end how to use it? Today will teach you to write a simple crawler.

The following personal blog website crawling the author of the first article as an example to obtain the title name, will teach you to learn a simple reptile.

The first step: Get page

#!/usr/bin/python
# coding: utf-8

import requests #引入包requests
link = "http://www.santostang.com/" #定义link为目标网页地址
# 定义请求头的浏览器代理,伪装成浏览器
headers = {'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'} 

r = requests.get(link, headers= headers) #请求网页
print (r.text)  #r.text是获取的网页内容代码

The code will be able to get the blog page of HTML code, HTML is the language used to describe a web page, that is behind the content of the page is rendered HTML code. If you are not familiar with HTML, you can go to w3school (http://www.w3school.com.cn/html/index.asp) learn about, probably spend a few hours to understand HTML.

In the above code, it is first introduced into the bag Requests import requests, after obtaining the page.

(1) First link is defined as the landing page address.

After (2) to define headers browser request header agent masquerading

(3) r is the Response reply to requests object from which we can get the desired information. r.text web content code is obtained.

Run the code results obtained as shown in FIG.
Here Insert Picture Description

The second step: extract the required data

#!/usr/bin/python
# coding: utf-8

import requests
from bs4 import BeautifulSoup     #从bs4这个库中导入BeautifulSoup

link = "http://www.santostang.com/"
headers = {'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'} 
r = requests.get(link, headers= headers)

soup = BeautifulSoup(r.text, "html.parser") #使用BeautifulSoup解析

#找到第一篇文章标题,定位到class是"post-title"的h1元素,提取a,提取a里面的字符串,strip()去除左右空格
title = soup.find("h1", class_="post-title").a.text.strip()
print (title)

After acquiring the entire page of HTML code, we need the title of the first article is extracted from the entire Web page.

This library is used here BeautifulSoup on page parsing, BeautifulSoup will be explained in detail in Chapter 4. First, the need to import library, and then into the HTML code objects soup, subsequently soup.find ( "h1", class _ = "post-title"). A.text.strip () to give the title of the first article, and print out

soup.find ( "h1", class _ = "post-title"). a.text.strip () mean, to find the first article titled, navigate to the class is "post-title" of h1 elements to extract a element, the element which extracts a character string, Strip () removed approximately spaces.

For starters, use BeautifulSoup extracted from the web page requires data easier to use.

So, how do we find the position title from the exact code so long?

Here we must Introducing the Chrome browser's "inspection (Inspect Element)" function of. Here are the steps to find the required elements.

Step 01 using the Chrome browser to open the blog home page www.santostang.com. Right-click on the web page, the shortcut menu, click "Check" command, shown in Figure 2-17.

Here Insert Picture Description

02 steps to review elements of the page appears as shown in Figure 2-18. Click the top left button mouse button, then click on the page you want the data, the following Elements will place the corresponding code is located, to navigate to the desired element.

Here Insert Picture Description

Figure 2-18 Inspect Element page

03 steps to find a place marked blue in the code for

echarts study notes (2) - the same multi-page chart . We can use soup.find ( "h1", class _ = "post-title"). A.text.strip () to extract the post title.

The third step: storing data

import requests
from bs4 import BeautifulSoup   #从bs4这个库中导入BeautifulSoup

link = "http://www.santostang.com/"
headers = {'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'} 
r = requests.get(link, headers= headers)

soup = BeautifulSoup(r.text, "html.parser") #使用BeautifulSoup解析
title = soup.find("h1", class_="post-title").a.text.strip()
print (title)

# 打开一个空白的txt,然后使用f.write写入刚刚的字符串title
with open('title_test.txt', "a+") as f:
    f.write(title)

Txt file to the local storage is very simple, with 2 lines of code in the second step on the basis of this string can be saved in text and stored locally. txt file address should be your Python file in the same folder.

Returns a folder, file title.txt open, the contents shown in Figure 2-19.
Here Insert Picture Description
The above content of this section is to introduce how to write a simple step reptiles, reptile want to learn more related content, this book can go to check oh "Python web crawler from entry to practice (2nd edition)"
Here Insert Picture Description

table of Contents

前言
第1章 网络爬虫入门1
1.1 为什么要学网络爬虫2
1.1.1 网络爬虫能带来什么好处2
1.1.2 能从网络上爬取什么数据3
1.1.3 应不应该学爬虫3
1.2 网络爬虫是否合法3
1.2.1 Robots协议4
1.2.2 网络爬虫的约束5
1.3 网络爬虫的基本议题6
1.3.1 Python爬虫的流程7
1.3.2 三个流程的技术实现7
第2章 编写第一个网络爬虫9
2.1 搭建Python平台10
2.1.1 Python的安装10
2.1.2 使用pip安装第三方库12
2.1.3 使用编辑器Jupyter 编程13
2.1.4 使用编辑器Pycharm编程15
2.2 Python 使用入门18
2.2.1 基本命令18
2.2.2 数据类型19
2.2.3 条件语句和循环语句21
2.2.4 函数23
2.2.5 面向对象编程24
2.2.6 错误处理28
2.3 编写第一个简单的爬虫29
2.3.1 第一步:获取页面29
2.3.2 第二步:提取需要的数据30
2.3.3 第三步:存储数据32
2.4 Python实践:基础巩固33
2.4.1 Python基础试题34
2.4.2 参考答案35
2.4.3 自我实践题38
第3章 静态网页抓取39
3.1 安装Requests40
3.2 获取响应内容40
3.3 定制Requests41
3.3.1 传递URL参数41
3.3.2 定制请求头42
3.3.3 发送POST请求43
3.3.4 超时44
3.4 Requests爬虫实践:TOP250电影数据44
3.4.1 网站分析45
3.4.2 项目实践45
3.4.3 自我实践题47
第4章 动态网页抓取48
4.1 动态抓取的实例49
4.2 解析真实地址抓取50
4.3 通过Selenium模拟浏览器抓取55
4.3.1 Selenium的安装与基本介绍55
4.3.2 Selenium的实践案例57
4.3.3 Selenium获取文章的所有评论58
4.3.4 Selenium的高级操作61
4.4 Selenium爬虫实践:深圳短租数据64
4.4.1 网站分析64
4.4.2 项目实践66
4.4.3 自我实践题69
第5章 解析网页70
5.1 使用正则表达式解析网页71
5.1.1 re.match方法71
5.1.2 re.search方法74
5.1.3 re.findall方法74
5.2 使用BeautifulSoup解析网页76
5.2.1 BeautifulSoup的安装76
5.2.2 使用BeautifulSoup获取博客标题77
5.2.3 BeautifulSoup的其他功能78
5.3 使用lxml解析网页82
5.3.1 lxml的安装82
5.3.2 使用lxml获取博客标题82
5.3.3 XPath的选取方法84
5.4 总结85
5.5 BeautifulSoup爬虫实践:房屋价格数据86
5.5.1 网站分析86
5.5.2 项目实践87
5.5.3 自我实践题89
第6章 数据存储90
6.1 基本存储:存储至TXT或CSV91
6.1.1 把数据存储至TXT91
6.1.2 把数据存储至CSV93
6.2 存储至MySQL数据库94
6.2.1 下载安装MySQL95
6.2.2 MySQL的基本操作99
6.2.3 Python操作MySQL数据库104
6.3 存储至MongoDB数据库106
6.3.1 下载安装MongoDB107
6.3.2 MongoDB的基本概念110
6.3.3 Python操作MongoDB数据库112
6.3.4 RoboMongo的安装与使用113
6.4 总结115
6.5 MongoDB爬虫实践:虎扑论坛116
6.5.1 网站分析116
6.5.2 项目实践117
6.5.3 自我实践题123
第7章 Scrapy框架124
7.1 Scrapy是什么125
7.1.1 Scrapy架构125
7.1.2 Scrapy数据流(Data Flow)126
7.1.3 选择Scrapy还是Requests+bs4127
7.2 安装Scrapy128
7.3 通过Scrapy抓取博客128
7.3.1 创建一个Scrapy项目128
7.3.2 获取博客网页并保存129
7.3.3 提取博客标题和链接数据131
7.3.4 存储博客标题和链接数据133
7.3.5 获取文章内容134
7.3.6 Scrapy的设置文件136
7.4 Scrapy爬虫实践:财经新闻数据137
7.4.1 网站分析137
7.4.2 项目实践138
7.4.3 自我实践题141
第8章 提升爬虫的速度142
8.1 并发和并行,同步和异步143
8.1.1 并发和并行143
8.1.2 同步和异步143
8.2 多线程爬虫144
8.2.1 简单的单线程爬虫145
8.2.2 学习Python多线程145
8.2.3 简单的多线程爬虫148
8.2.4 使用Queue的多线程爬虫150
8.3 多进程爬虫153
8.3.1 使用multiprocessing的多进程爬虫153
8.3.2 使用Pool + Queue的多进程爬虫155
8.4 多协程爬虫158
8.5 总结160
第9章 反爬虫问题163
9.1 为什么会被反爬虫164
9.2 反爬虫的方式有哪些164
9.2.1 不返回网页165
9.2.2 返回非目标网页165
9.2.3 获取数据变难166
9.3 如何“反反爬虫”167
9.3.1 修改请求头167
9.3.2 修改爬虫的间隔时间168
9.3.3 使用代理171
9.3.4 更换IP地址172
9.3.5 登录获取数据172
9.4 总结172
第10章 解决中文乱码173
10.1 什么是字符编码174
10.2 Python的字符编码176
10.3 解决中文编码问题179
10.3.1 问题1:获取网站的中文显示乱码179
10.3.2 问题2:非法字符抛出异常180
10.3.3 问题3:网页使用gzip压缩181
10.3.4 问题4:读写文件的中文乱码182
10.4 总结184
第11章 登录与验证码处理185
11.1 处理登录表单186
11.1.1 处理登录表单186
11.1.2 处理cookies,让网页记住你的登录190
11.1.3 完整的登录代码193
11.2 验证码的处理194
11.2.1 如何使用验证码验证195
11.2.2 人工方法处理验证码197
11.2.3 OCR处理验证码200
11.3 总结203
第12章 服务器采集204

此书已加入到VIP会员卡,只要购买VIP会员卡即可免费阅读上百本电子书,这张VIP卡除了免费让你读书,还有更多的权益等你来领,往下↓拉

购买链接:https://t.csdnimg.cn/nXMa 买这个卡后不仅可以享受价值上万元的电子书免费阅读权限

还可以享受到以下这些权益呢:

400次在CSDN下载频道下载资源的机会

1000多门的课程可以免费看

购买CSDN学院课程可享受9折专属优惠

CSDN网站免广告哦

Released nine original articles · won praise 18 · views 3242

Guess you like

Origin blog.csdn.net/weixin_37649168/article/details/104265388