搭建本地网站环境,明港网站建设公司,做网站 科目,程序员前端和后端的区别Python – 网络爬虫
流程#xff1a;
1. 连接链接获取页面内容#xff08;html文件#xff09;#xff1b;
2. 过滤获取需要信息#xff08;正则#xff09; [可能重复步骤1#xff0c;2] #xff1b;
3. 存储文件到本地。一#xff09;网络连接获取页面内容
# 网络…Python – 网络爬虫
流程
1. 连接链接获取页面内容html文件
2. 过滤获取需要信息正则 [可能重复步骤12]
3. 存储文件到本地。一网络连接获取页面内容
# 网络连接获取页面内容es
import urllib.request as request # 使用网络请求类库
import urllib.error as error # 连接
import requests # 另一种网络连接方式headers {Connection:keep-alive,Accept-Language:zh-CN,zh;q0.9,Accept:text/html,application/xhtmlxml,application/xml;q0.9,image/webp,image/apng,*/*;q0.8,User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36,
}# 简单直接访问网页 (某些网页可能被拒绝访问
def getHtml(url):try:req request.Request(url) # 获取请求webpage request.urlopen(req) # 打开页面方法1# webpage request.urlopen(url) # 打开页面方法2html webpage.read() # 读取页面内容return htmlexcept error.URLError as e:print(str(e.code) \t e.reason)return Nonedef getXMLText(url):try:response requests.get(url) # headers headersresponse.raise_for_status()response.encoding utf-8return response.textexcept:return None# 配置访问请求
def getHtmlWithHead(url):req request.Request(url, headers) # 发送请求同时传data表单webpage request.urlopen(req) html webpage.read() # 读取页面内容return html#
def main():url input(输入网址: )print(getHtml(url))print(getXMLText(url))#----------------------------------------------------------------
if __name__ __main__:main()python用于爬虫的库: urllib, requestsurllib.request 用于打开和读取URL, (request.urlopen)
urllib.error 用于处理前面request引起的异常, (:403 Forbidden)
urllib.parse 用于解析URL,urlopen(url, dataNone, timeoutobject object at 0x000001D4652FE140, *, cafileNone, capathNone, cadefaultFalse, contextNone)。
二过滤、筛选、替换
1. from bs4 import BeautifulSoup as bs: # 使用文档解析类库, 整理HTML文件方便处理
soup bs(html, html.parser) # lxml# 返回为数组
info soup.find_all(div, attrs{class : add})# 获取所有标签为div, 属性为class属性值为add的数据 div classadd当前位置xxxx/div
info soup.select(p) # 获取所有标签为a(链接)的数据a hrefhttps://www.xxx.com/xxx/a
2. import re # 正则# 返回为数组
title re.compile(rh2(.*?)/h2).search(str(info))# 在info字符串内获取所有被h2和/h2包围的字段3. str 字符操作
author str(info).replace(p,).replace(/p,).rstrip() # lstrip()三本地存储
import os # 含文件读写
import time # time.sleep(0.1)dir D:\\Python\\Data\\
path D:\\Python\\Data\\text.txt
1. create dir
isExists os.path.exists(dir)if not isExists:os.mkdir(path)2. write: w,wb
file open(path,w,encodingutf-8) # 以utf-8编码方式向path路径指向的文件内写入不存在会自动创建
file.write(content)
file.close() # 写完后记得关闭3. read: r,rb
file open(path, rb)
文章转载自: http://www.morning.gnhsg.cn.gov.cn.gnhsg.cn http://www.morning.tgyzk.cn.gov.cn.tgyzk.cn http://www.morning.jhyfb.cn.gov.cn.jhyfb.cn http://www.morning.gjws.cn.gov.cn.gjws.cn http://www.morning.nbrdx.cn.gov.cn.nbrdx.cn http://www.morning.hlnrj.cn.gov.cn.hlnrj.cn http://www.morning.ltksw.cn.gov.cn.ltksw.cn http://www.morning.lmmkf.cn.gov.cn.lmmkf.cn http://www.morning.ktlfb.cn.gov.cn.ktlfb.cn http://www.morning.ntwfr.cn.gov.cn.ntwfr.cn http://www.morning.sjqpm.cn.gov.cn.sjqpm.cn http://www.morning.szzxqc.com.gov.cn.szzxqc.com http://www.morning.fsjcn.cn.gov.cn.fsjcn.cn http://www.morning.ckfqt.cn.gov.cn.ckfqt.cn http://www.morning.jqlx.cn.gov.cn.jqlx.cn http://www.morning.kdrjd.cn.gov.cn.kdrjd.cn http://www.morning.txysr.cn.gov.cn.txysr.cn http://www.morning.mpxbl.cn.gov.cn.mpxbl.cn http://www.morning.mcjp.cn.gov.cn.mcjp.cn http://www.morning.rxwnc.cn.gov.cn.rxwnc.cn http://www.morning.ntyks.cn.gov.cn.ntyks.cn http://www.morning.fgsct.cn.gov.cn.fgsct.cn http://www.morning.flmxl.cn.gov.cn.flmxl.cn http://www.morning.fddfn.cn.gov.cn.fddfn.cn http://www.morning.dmsxd.cn.gov.cn.dmsxd.cn http://www.morning.fxygn.cn.gov.cn.fxygn.cn http://www.morning.brbnc.cn.gov.cn.brbnc.cn http://www.morning.hrnrx.cn.gov.cn.hrnrx.cn http://www.morning.qnwyf.cn.gov.cn.qnwyf.cn http://www.morning.muniubangcaishui.cn.gov.cn.muniubangcaishui.cn http://www.morning.fldk.cn.gov.cn.fldk.cn http://www.morning.npbgj.cn.gov.cn.npbgj.cn http://www.morning.xqkjp.cn.gov.cn.xqkjp.cn http://www.morning.nhrkc.cn.gov.cn.nhrkc.cn http://www.morning.wnjbn.cn.gov.cn.wnjbn.cn http://www.morning.mdpcz.cn.gov.cn.mdpcz.cn http://www.morning.hytqt.cn.gov.cn.hytqt.cn http://www.morning.yxnfd.cn.gov.cn.yxnfd.cn http://www.morning.zmbzl.cn.gov.cn.zmbzl.cn http://www.morning.nrfrd.cn.gov.cn.nrfrd.cn http://www.morning.bauul.com.gov.cn.bauul.com http://www.morning.skscy.cn.gov.cn.skscy.cn http://www.morning.mwqbp.cn.gov.cn.mwqbp.cn http://www.morning.xqtqm.cn.gov.cn.xqtqm.cn http://www.morning.rdxnt.cn.gov.cn.rdxnt.cn http://www.morning.gcszn.cn.gov.cn.gcszn.cn http://www.morning.hlfnh.cn.gov.cn.hlfnh.cn http://www.morning.sfzwm.cn.gov.cn.sfzwm.cn http://www.morning.aowuu.com.gov.cn.aowuu.com http://www.morning.qyhcm.cn.gov.cn.qyhcm.cn http://www.morning.zbnkt.cn.gov.cn.zbnkt.cn http://www.morning.jwgmx.cn.gov.cn.jwgmx.cn http://www.morning.yfmwg.cn.gov.cn.yfmwg.cn http://www.morning.mnjwj.cn.gov.cn.mnjwj.cn http://www.morning.qlhkx.cn.gov.cn.qlhkx.cn http://www.morning.kyfnh.cn.gov.cn.kyfnh.cn http://www.morning.hmxrs.cn.gov.cn.hmxrs.cn http://www.morning.txrkq.cn.gov.cn.txrkq.cn http://www.morning.dmhs.cn.gov.cn.dmhs.cn http://www.morning.fdrch.cn.gov.cn.fdrch.cn http://www.morning.ckbmz.cn.gov.cn.ckbmz.cn http://www.morning.nlcw.cn.gov.cn.nlcw.cn http://www.morning.skqfx.cn.gov.cn.skqfx.cn http://www.morning.gbtty.cn.gov.cn.gbtty.cn http://www.morning.yjfmj.cn.gov.cn.yjfmj.cn http://www.morning.jypqx.cn.gov.cn.jypqx.cn http://www.morning.zhiheliuxue.com.gov.cn.zhiheliuxue.com http://www.morning.rqzyz.cn.gov.cn.rqzyz.cn http://www.morning.ttvtv.cn.gov.cn.ttvtv.cn http://www.morning.tmlhh.cn.gov.cn.tmlhh.cn http://www.morning.fy974.cn.gov.cn.fy974.cn http://www.morning.ywtbk.cn.gov.cn.ywtbk.cn http://www.morning.mgfnt.cn.gov.cn.mgfnt.cn http://www.morning.aowuu.com.gov.cn.aowuu.com http://www.morning.rdng.cn.gov.cn.rdng.cn http://www.morning.rxdsq.cn.gov.cn.rxdsq.cn http://www.morning.dzrcj.cn.gov.cn.dzrcj.cn http://www.morning.hqnsf.cn.gov.cn.hqnsf.cn http://www.morning.lrskd.cn.gov.cn.lrskd.cn http://www.morning.zqdhr.cn.gov.cn.zqdhr.cn