笔记之《用python写网络爬虫》

news/2024/7/19 11:50:14 标签: 爬虫, 数据库, python

1 .3 背景调研

robots. txt

 

Robots协议(也称为爬虫协议、机器人协议等)的全称是“网络爬虫排除标准”(Robots Exclusion Protocol),网站通过Robots协议告诉搜索引擎哪些页面可以抓取,哪些页面不能抓取。

WHOIS

whois是用来查询域名的IP以及所有者等信息的传输协议。简单说,whois就是一个用来查询域名是否已经被注册,以及注册域名的详细信息的数据库(如域名所有人、域名注册商)。

1.3.1 检查robots.txt

  crawler英[ˈkrɔ:lə(r)]
  美[ˈkrɔlɚ]
  n.爬行者,爬行动物

1.3.4 识别网站所用技术

检查网站 构建的技术类型builtwith 模块

 

>> import builtwith
>> builtwith.parse('http://exaple.webscraping.com') 

1.3.5 寻找网站所有者 

为了找到网站的所有者,我们可以使用WHOIS协议查询域名的注册者是谁 

pip install python-whois

1.4 编写第一个网络爬虫

· 爬取网站地图;
· 遍历每个网页的数据库ID;
· 跟踪网页链接  

1.4.1 下载网页 

4xx 错误发生在请求存在问题时,而5xx 错误则发生在服务端存在问题时。

1 . 重试下载

2. 设置用户代理 

import urllib2

def download(url,user_agent='wswp',num_retries = 2):
url = 'http://httpstat.us/500'
print 'Downloading',url
headers = {User-agent:user_agent}
request = urllib2.Request(url,headers = headers)

try:
html = urllib2.urlopen(url).read()
except URLError as e:
print 'Downloading error',e.reason
html = None
if num_retries > 0:
if hasattr(e,'code') and 500<=e.code<600:
return download(url,num_retries-1)
return html

if __name__ == '__main__':
download('http://httpstat.us/500',num_retries =2)

注:1.NameError: global name 'User' is not defined

  2.hasattr(object, name)---作用:判断对象object是否包含名为name的特性

  3.recursively   递归的 

1.4.2 网站地图爬虫

import urllib2


def crawel_sitemap(url):
sitemap = download(url)
links = re.findall('<loc>(.*?)<./loc>',sitemap)

for link in links:
html = download(link)

if __name__ == '__main__':
crawel_sitemap('http://exale.webscraping.com/sitemap.xl')

注:1. .*?   *? 重复任意次,但尽可能少重复--非贪婪匹配

  2.extact    英[ˈekstrækt]  美[ɪkˈstrækt]

          vt.提取; (费力地) 拔出; 选取; 获得;

  3.NameError: global name 'download' is not defined

 

 

 

 

转载于:https://www.cnblogs.com/liuqi-beijing/p/6386386.html


http://www.niftyadmin.cn/n/932997.html

相关文章

Vault 不同版本的API的异同

大家知道&#xff0c;Autodesk Vault 2014有几个版本&#xff0c;依次为( Basic, Workgroup, Professional),不同版本的功能不相同,关于Vault产品功能的不同之处可以在Vault的产品主页看到。但经常会有客户问到Vault的API在不同版本下有什么不同吗&#xff1f; 或者要实现某个功…

php传递post参数乱码问题,PHP基于curl post实现发送url及相关中文乱码问题解决方法...

本文实例讲述了PHP基于curl post实现发送url及相关中文乱码问题解决方法。分享给大家供大家参考&#xff0c;具体如下&#xff1a;这个问题困扰我几天了&#xff0c;发送的指定网址的url参数&#xff0c;中文总是乱码&#xff0c;指定网址是utf8编码的&#xff0c;我发送的也是…

hdu3652 B-number

题意&#xff1a; 求1~n中出现“13”并且能被13整除的数的个数。(1 < n < 1000000000). 思路&#xff1a; 数位dp。 实现&#xff1a; 1 #include <iostream>2 #include <cstdio>3 #include <cstring>4 using namespace std;5 6 int num[15], n;7 8 in…

winform 导出datagridview 到excel

如果电脑上没有Microsoft.Office.Interop.Excel.dll去找DLL下载站下载即可 需要先导入这个dll的引用 呈上代码&#xff1a;using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using Syste…

php 浏览器 保存网页图片,PHP使用CURL抓取网页并将网页保存为图片

php 是不行的, 可以用这个 CutyCapt.exe因为 php 用 exec() 函数是可以执行 终端命令的这里的代码 用的 别人的,不知道能不能用,这种代码没必要自己写,理解思路很重要//设置页面编码header("Content-Type:text/html; charsetutf-8");//设置运行不超时set_time_limit(…

MVC ---- T4(1)

T4 模板编辑插件&#xff1a;tangibleT4EditorPlusModellingToolsVS2012.msi 下载地址&#xff1a;http://t4-editor.tangible-engineering.com/Download_T4Editor_Plus_ModelingTools.html $(SolutionDir)&#xff1a;当前项目所在解决方案目录$(ProjectDir)&#xff1a;当前…

AtCoder Beginner Contest 055题解

A.当a1就把a改成14&#xff0c;b1就把b改成14&#xff0c;然后比较a&#xff0c;b大小即可。 #include <iostream> #include <algorithm> #include <cstdio> using namespace std; int a, b; int main() {cin >> a >> b;if(a1) a14; if(b1) b14;…

python php 序列化,Python phpserialize包_程序模块 - PyPI - Python中文网

用法>>> from phpserialize import *>>> obj dumps("Hello World")>>> loads(obj)Hello World因为php不知道列表的概念像php中的散列映射一样序列化。事实上序列化列表的反向值是dict:>>> loads(dumps(range(2))){0: 0, 1: 1}如…