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 ...
随机推荐
- 6、python中的字符串
最早的编码为ascii码,共256个符号.UTF-8是国际通用编码,全面支持中文,以一个字节表示英文,以三个字节表示一个中文以及其他语言:GB2312是我国自己定制的中文编码标准,使用1个字节表示英文 ...
- hpuoj--校赛--送给新生的礼物(水题)
问题 A: 感恩节KK专场--送给新生的礼物 时间限制: 1 Sec 内存限制: 128 MB 提交: 631 解决: 187 [提交][状态][讨论版] 题目描述 学长KK要送给学弟学妹们礼物, ...
- Objects are mutable
We can change the state of an object by making an assignment to one of its attributes. For example, ...
- Boostrap零散
12 row 是核心控件 class="form-control" 弹窗口<input data-toggle="modal" data-target=& ...
- Java 类和对象3
编写Java应用程序.首先,定义描述学生的类——Student,包括学号(int).姓名(String).年龄(int)等属性:二个方法:Student(int stuNo,String name,i ...
- PostgreSQL Replication之第七章 理解Linux高可用(6)
7.6 PostgreSQL和高可用性 数据库是我们日常数字生活的一部分,并期望它们快速工作. 您浏览网上论坛吗?那个帖子在数据库中.您看医生吗?您的医疗记录在数据库中.您在网上购物吗?那个货物,您的 ...
- E. Reachability from the Capital(tarjan+dfs)
求联通分量个数,在dfs一次 #include <iostream> #include <algorithm> #include <cstring> #includ ...
- NodeJS学习笔记 (26)命令行设计-repl
https://github.com/chyingp/nodejs-learning-guide
- ArchLinux 设置时间同步和硬件时间同步错误 No usable clock interface found
笔记本不知道怎么了,总是时间对不上 硬件时间也设置不了,只能时间同步了 手动时间同步 ntpdate即可,ntp服务器在这里用这两个就好了 cn.ntp.org.cn 或 edu.ntp.org.cn ...
- SweetAlert的入门
在做后台管理系统,在用户交互这块(弹窗.提示相关),用了一款还不错的插件SweetAlert(一款原生js提示框,允许自定义,支持设置提示框标题.提示类型.确认取消按钮文本.点击后回调函数等等), 效 ...