Python 爬取单个网页所需要加载的地址和CSS、JS文件地址
Python 爬取单个网页所需要加载的URL地址和CSS、JS文件地址
通过学习Python爬虫,知道根据正式表达式匹配查找到所需要的内容(标题、图片、文章等等)。而我从测试的角度去使用Python爬虫,希望爬取到访问该网页所需要的CSS、JS、URL,然后去请求这些地址,根据响应的状态码判断是否都可以成功访问。
代码
'''
Created on 2017-08-02
@author: Lebb
'''
import sys
import urllib2
import re
reload(sys)
sys.setdefaultencoding('utf-8')
url = "https://www.szrtc.cn/"
http = "http"
request = urllib2.Request(url,headers=Headers)
responsecode = None
errorcount = 0
itemurl = url
def getResponse():
try:
response = urllib2.urlopen(request)
except urllib2.HTTPError,he:
print he.code
except urllib2.URLError,ue:
print ue.reason
else :
return response.read().decode('utf-8')
def getUrl():
html = getResponse()
patterncss ='<link href="(.*?)"'
patternjs = '<script src="(.*?)"'
patternimg = '<img src="(.*?)"'
patternpage = '<a.*?href="(.*?)"'
patternonclick = "openQuestion.*?'(.*?)'"
href = re.compile(patterncss, re.S).findall(html)
href += re.compile(patternimg, re.S).findall(html)
href += re.compile(patternpage, re.S).findall(html)
href += re.compile(patternjs, re.S).findall(html)
href += re.compile(patternonclick, re.S).findall(html)
return href
def reasonCode():
global errorcount
itemurl = getUrl()
for item1 in itemurl:
if http in item1:
sendurl = item1
else:
sendurl = url + item1
try:
print sendurl
responseurl = urllib2.urlopen(sendurl,timeout=8)
except urllib2.HTTPError,he:
responsecode = he.code
errorcount += 1
except urllib2.URLError,ue:
responsecode = ue.reason
errorcount += 1
else:
responsecode = responseurl.getcode()
if(responsecode != 200):
errorcount += 1
print responsecode
#return responsecode
print errorcount
运行的结果:
错误截图:
实际上这条请求复制到浏览器是可以访问的,但是Python 的urllib2访问时,因为请求带中文参数,没有进行编码转换,导致报400错误。
尝试在代码中加入utf-8,还是没有效果,仍然报错。
这个问题先记下来,后面去找到其他解决办法
Python 爬取单个网页所需要加载的地址和CSS、JS文件地址的更多相关文章
- python爬取某个网页的图片-如百度贴吧
python爬取某个网页的图片-如百度贴吧 作者:vpoet mail:vpoet_sir@163.com 注:随意copy,不用告诉我 #coding:utf-8 import urllib imp ...
- python爬取动态网页数据,详解
原理:动态网页,即用js代码实现动态加载数据,就是可以根据用户的行为,自动访问服务器请求数据,重点就是:请求数据,那么怎么用python获取这个数据了? 浏览器请求数据方式:浏览器向服务器的api(例 ...
- python爬取基础网页图片
python基础爬虫总结 1.爬取信息原理 与浏览器客户端类似,向网站的服务器发送一个请求,该请求一般是url,也就是网址.之后服务器响应一个html页面给客户端,当然也有其他数据类型的信息,这些就是 ...
- python爬取豆瓣小组700+话题加回复啦啦啦python open file with a variable name
需求:爬取豆瓣小组所有话题(话题title,内容,作者,发布时间),及回复(最佳回复,普通回复,回复_回复,翻页回复,0回复) 解决:1. 先爬取小组下,所有的主题链接,通过定位nextpage翻页获 ...
- 预加载(图片,css ,js)
图片预加载 new Image().src = 'http://img1.t.sinajs.cn/t35/skin/skin_008/skin.css'; //新浪(4) 非ie下预加载(js,css ...
- Python抓取单个网页中所有的PDF文档
Github博文地址,此处更新可能不是很及时. 1.背景 最近发现算法以及数据结构落下了不少(其实还是大学没怎么好好学,囧rz),考虑到最近的项目结构越来越复杂了,用它来练练思路,就打算复习下数据结构 ...
- Python 爬取bangumi网页信息
1.数据库连接池 #######db.py########## import time import pymysql import threading from DBUtils.PooledDB im ...
- python爬取动态网页2,从JavaScript文件读取内容
import requests import json head = {"user-agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) ...
- python爬取凤凰网站的新闻,及其链接地址,来源,时间和内容,用selenium自动化和requests处理数据
有写规则需要自己定义判断. import requests from selenium import webdriver import time def grasp(urlT): driver = w ...
随机推荐
- uva_11997,K Smallest Sums优先队列
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #inclu ...
- Redis的好处知识
参考文章 http://www.cnblogs.com/wupeiqi/articles/5132791.html 使用Redis有哪些好处? () 速度快,因为数据存在内存中,类似于HashMap, ...
- Android框架-Volley(四)
经过前三篇文章的学习,Volley的用法我们已经掌握的差不多了,但是对于Volley的工作原理,恐怕有很多朋友还不是很清楚.因此,本篇文章中我们就来一起阅读一下Volley的源码,将它的工作流程整体地 ...
- Servlet设置Cookie无效
项目中保存用户信息用到了Cookie,之前没有太注意,今天怎么设置Cookie都无效,断点跟了无数遍,都没有找出问题所在,明明发送Cookie的代码都有执行,可是愣是找不到Cookie发送到哪里去了, ...
- [HAOI2007]理想的正方形 单调队列 暴力
Code: #include<cstdio> #include<queue> #include<algorithm> using namespace std; #d ...
- Tomcat IO阻塞异常
tomcat的maxThreads.acceptCount(最大线程数.最大排队数) tomcat 的Connector配置如下 <Connector port="8080" ...
- letCode(771 Jewels and Stones )
问题描述: You're given strings J representing the types of stones that are jewels, and S representing th ...
- numpy中list array matrix比较
用python中的numpy包的时候不小心踩了array和matrix的大坑,又引申一下比较list array matrix之间的异同.数据结构(Data Structures)基本上人如其名——它 ...
- Rsyslog比较详细的
http://blog.csdn.net/fishmai/article/details/51842340
- 在启动php时,无法启动此程序,由于计算机中丢失MSVCR110.dll的解决方法
在启动php时,运行RunHiddenconsole.exe php-cgi.exe -b 127.0.0.1:9000 -c时,出现错误:无法启动此程序,由于计算机中丢失MSVCR110.dll 方 ...