大家好,又见面了,我是你们的朋友全栈君。
本文参考网上的资料,编写简单的Python编写网络爬虫,做了网页内容的抓取,分析出链接的url并抓取。
1.环境准备
安装python3,PyCharm开发环境
2.牛刀小试:
# encoding:UTF-8
import urllib.request #引入urllib工具
url = "http://www.baidu.com" #Url地址
data = urllib.request.urlopen(url).read() # 使用urllib工具获取url的页面内容
data = data.decode('UTF-8') #将格式转换为UTF-8格式
print(data) # 打印获取的页面信息
<html><head> <meta http-equiv="content-type" content="text/html;charset=utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=Edge"> <meta content="always" name="referrer"> <meta name="theme-color" content="#2932e1"> <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" /> <link rel="search" type="application/opensearchdescription+xml" href="/content-search.xml" title="百度搜索" /> <link rel="icon" sizes="any" mask href="//www.baidu.com/img/baidu.svg"> <link rel="dns-prefetch" href="//s1.bdstatic.com"/> <link rel="dns-prefetch" href="//t1.baidu.com"/> <link rel="dns-prefetch" href="//t2.baidu.com"/> <link rel="dns-prefetch" href="//t3.baidu.com"/> <link rel="dns-prefetch" href="//t10.baidu.com"/> <link rel="dns-prefetch" href="//t11.baidu.com"/> <link rel="dns-prefetch" href="//t12.baidu.com"/> <link rel="dns-prefetch" href="//b1.bdstatic.com"/> <title>百度一下,你就知道</title> <style id="css_index" index="index" type="text/css">html,body{height:100%}html{overflow-y:auto}body{font:12px arial;text-align:;background:#fff}body,p,form,ul,li{margin:0;padding:0;list-style:none}body,form,#fm{position:relative}td{text-align:left}img{border:0}a{color:#00c} ........ ........ 由于内容过多,此处省略<script>if(navigator.cookieEnabled){ document.cookie="NOJS=;expires=Sat, 01 Jan 2000 00:00:00 GMT";}</script></body></html>
3.抓取页面中的链接页面
import re
import urllib.request
import urllib
from collections import deque #引入队列
queue = deque() #定义队列,保存待处理的url
visited = set() # 保存已抓取的url
url = 'http://www.csdn.net' # 入口页面, 可以换成别的
queue.append(url) # 添加到待处理的队列中
cnt = 0 # 已抓取数
while queue:
url = queue.popleft() # 获取队列队首元素
visited.add(url) # 添加到已经访问set中
print('已经抓取: ' + str(cnt) + ' 正在抓取 <--- ' + url)
print('已经抓取的url:', visited)
cnt += 1
try:
urlop = urllib.request.urlopen(url) #抓取
except:
print('抓取: ' + url + ' 异常 ')
continue
if 'html' not in urlop.getheader('Content-Type'): # 判断是否是html的url
continue
# 避免程序异常中止, 用try...catch处理异常
try:
data = urlop.read().decode('utf-8')
except:
continue
# 正则表达式提取页面中所有队列, 并判断是否已经访问过, 如果没有访问过,则加入待抓取队列
linkre = re.compile('href="(.+?)"')
for x in linkre.findall(data):
if 'http' in x and x not in visited:
queue.append(x)
print('加入队列 ---> ' + x)
已经抓取: 0 正在抓取 <--- http://www.csdn.net已经抓取的url: {'http://www.csdn.net'}加入队列 ---> http://c.csdnimg.cn/www/css/csdn_common.css加入队列 ---> http://c.csdnimg.cn/public/favicon.ico加入队列 ---> https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn ...... 此处省略加入队列 ---> http://powerlinux.csdn 加入队列 ---> http://www.csdn.net/tag/jquery 加入队列 ---> http://www.csdn.net/company/icp.html加入队列 ---> http://www.csdn.net/company/pifu.html加入队列 ---> http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001032100010加入队列 ---> http://c.csdnimg.cn/comm_ask/css/ask_float_block.css已经抓取: 1 正在抓取 <--- http://c.csdnimg.cn/www/css/csdn_common.css已经抓取的url: {'http://c.csdnimg.cn/www/css/csdn_common.css', 'http://www.csdn.net'}已经抓取: 2 正在抓取 <--- http://c.csdnimg.cn/public/favicon.ico已经抓取的url: {'http://c.csdnimg.cn/www/css/csdn_common.css', 'http://www.csdn.net', 'http://c.csdnimg.cn/public/favicon.ico'}已经抓取: 3 正在抓取 <--- https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn已经抓取的url: {'http://c.csdnimg.cn/www/css/csdn_common.css', 'https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn', 'http://www.csdn.net', 'http://c.csdnimg.cn/public/favicon.ico'}加入队列 ---> http://edu.csdn.net/yearPromotion/detail/3加入队列 ---> /account/fpwd?action=forgotpassword&service=http%3A%2F%2Fmy.csdn.net%2Fmy%2Fmycsdn加入队列 ---> https://api.weibo.com/oauth2/authorize?client_id=2601122390&response_type=code&redirect_uri=https%3A%2F%2Fpassport.csdn.net%2Faccount%2Flogin%3Foauth_provider%3DSinaWeiboProvider加入队列 ---> https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=75fvsy4v01jw1s&redirect_uri=https%3A%2F%2Fpassport.csdn.net%2Faccount%2Flogin%3Foauth_provider%3DLinkedInProvider&state=DCEEFWF45453sdffef424加入队列 ---> https://openapi.baidu.com/oauth/2.0/authorize?response_type=code&client_id=cePqkUpKCBrcnQtARTNPxxQG&redirect_uri=https%3A%2F%2Fpassport.csdn.net%2Faccount%2Flogin%3Foauth_provider%3DBaiduProvider加入队列 ---> https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=100270989&redirect_uri=https%3A%2F%2Fpassport.csdn.net%2Faccount%2Flogin%3Foauth_provider%3DQQProvider&state=test加入队列 ---> https://github.com/login/oauth/authorize?client_id=4bceac0b4d39cf045157&redirect_uri=https%3A%2F%2Fpassport.csdn.net%2Faccount%2Flogin%3Foauth_provider%3DGitHubProvider加入队列 ---> /account/mobileregister?action=mobileRegisterView&service=http%3A%2F%2Fmy.csdn.net%2Fmy%2Fmycsdn已经抓取: 4 正在抓取 <--- http://passport.csdn.net/account/mobileregister?action=mobileRegister已经抓取的url: {'http://c.csdnimg.cn/www/css/csdn_common.css', 'http://www.csdn.net', 'http://passport.csdn.net/account/mobileregister?action=mobileRegister', 'http://c.csdnimg.cn/public/favicon.ico', 'https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn'}加入队列 ---> https://csdnimg.cn/public/common/toolbar/css/index.css加入队列 ---> /account/register?action=registerView&service=http://www.csdn.net已经抓取: 5 正在抓取 <--- https://passport.csdn.net/help/faq已经抓取的url: {'http://c.csdnimg.cn/www/css/csdn_common.css', 'http://www.csdn.net', 'http://passport.csdn.net/account/mobileregister?action=mobileRegister', 'http://c.csdnimg.cn/public/favicon.ico', 'https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn', 'https://passport.csdn.net/help/faq'}加入队列 ---> https://passport.csdn.net/account/changepassword加入队列 ---> https://passport.csdn.net/account/forgotpassword加入队列 ---> https://passport.csdn.net/account/forgotpassword已经抓取: 6 正在抓取 <--- http://news.csdn.net已经抓取的url: {'http://c.csdnimg.cn/www/css/csdn_common.css', 'http://www.csdn.net', 'http://passport.csdn.net/account/mobileregister?action=mobileRegister', 'http://c.csdnimg.cn/public/favicon.ico', 'http://news.csdn.net', 'https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn', 'https://passport.csdn.net/help/faq'}加入队列 ---> http://c.csdnimg.cn/public/common/toolbar/css/index.css
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/149561.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...