企业网站模板 演示,免费建站还用学做网站吗,杭州响应式网站案例,wordpress人才市场文章目录 导入相应的库正确地设置代码的基础部分设置循环遍历遍历URL保存图片和文档全部代码即详细注释 下面是通过requests库来对ajax页面进行爬取的案例#xff0c;与正常页面不同#xff0c;这里我们获取url的方式也会不同#xff0c;这里我们通过爬取一个简单的ajax小说… 文章目录 导入相应的库正确地设置代码的基础部分设置循环遍历遍历URL保存图片和文档全部代码即详细注释 下面是通过requests库来对ajax页面进行爬取的案例与正常页面不同这里我们获取url的方式也会不同这里我们通过爬取一个简单的ajax小说页面来为大家讲解。注结尾附赠全部代码与详细注释 导入相应的库
爬取数据必须有相应的库这里我们使用爬虫脚本中常用的几个Python库os.path、fake_useragent 和 requests。 1.os.path
这个模块主要用于处理文件和目录的路径。它提供了一系列的功能来进行路径的拼接、拆分、查询等操作以确保路径的跨平台兼容性比如Windows和Unix/Linux系统的路径分隔符不同。在爬虫中os.path 通常用于构建本地文件系统的路径以便保存从网络上下载的图片、文本数据等。
2.fake_useragent
这个库用于生成随机的、看起来像是真实浏览器的User-Agent字符串。User-Agent是一个在HTTP请求中发送给服务器的头部信息它告诉服务器发起请求的客户端通常是浏览器的类型、版本和操作系统等信息。在爬虫中由于许多网站会检查User-Agent来识别爬虫请求并阻止它们因此使用fake_useragent可以帮助爬虫绕过这种简单的反爬虫机制。
3.requests
requests是Python中非常流行的HTTP库用于发送HTTP/1.1请求。它提供了一个简单易用的API用于处理各种HTTP请求如GET、POST、PUT、DELETE等。在爬虫中requests库是发送网络请求并获取响应的主要工具。它支持会话Session对象、HTTPS请求、文件上传、Cookie处理、重定向、连接池等功能非常适合用于构建复杂的爬虫系统。
import os.path
import fake_useragent
import requests 正确地设置代码的基础部分
这里我们生成一个随机的User-Agent、检查并创建目录以便储存爬取的图片、以及打开或创建一个文本文件来保存数据。
import os.path
import fake_useragent
import requests # 判断是否是直接运行该脚本
if __name__ __main__: head {User-Agent: fake_useragent.UserAgent().random} if not os.path.exists(./biqugePic): os.mkdir(./biqugePic) f open(./biquge.txt, w, encodingutf8) 设置循环遍历
循环遍历URL这里为大家提供具体url的获取方法并循环了1至9页的数据为大家做案例并发送了带有随机User-Agent的GET请求。这是爬虫中常见的做法用于从网站的不同页面获取数据。 for i in range(1, 10): url fhttps://www.bqgui.cc/json?sortid1page{i} resp requests.get(url, headershead) 首先进入网页点击F12打开自定义与控制工具点击fecth/XHR此时显示部分为空白。 这个时候我们滚动鼠标滚轮就会出现相应的url这里的https://www.bqgui.cc/json?sortid1page2其中尾部2表示滚轮页面第二页想要获取1至9我们只需要进行一个简单的循环遍历即可。 遍历URL
遍历从URL获取的JSON响应该响应包含多个项目。对于每个项目您都提取了图片URL、文章名、作者和简介并计划将这些信息打印到控制台以及下载图片和保存文本信息到文件。 for item in resp.json(): # 从每个JSON对象中提取所需的信息 img_url item[url_img] articlename item[articlename] author item[author] intro item[intro] # 打印提取的信息到控制台 print(img_url, author, articlename, intro) # 发送另一个GET请求到图片URL以获取图片内容 img_rest requests.get(img_url, headershead) 保存图片和文档
设置代码来保存图片到以文章名命名的文件中并将作者、文章名和简介信息写入到./biquge.txt文件中。
with open(f./biqugePic/{articlename}.jpg, wb) as fp:
# 将图片内容写入文件
fp.write(img_rest.content)
# 将作者、文章名和简介信息写入到./biquge.txt文件中
f.write(author # articlename # intro \n)全部代码即详细注释
import os.path
import fake_useragent
import requests # 判断是否是直接运行该脚本
if __name__ __main__: # 创建一个包含随机User-Agent的HTTP请求头 head {User-Agent: fake_useragent.UserAgent().random} # 检查是否存在名为./biqugePic的文件夹如果不存在则创建它 if not os.path.exists(./biqugePic): os.mkdir(./biqugePic) # 以写入模式打开或创建一个名为./biquge.txt的文件用于保存数据 f open(./biquge.txt, w, encodingutf8) # 循环从第1页到第9页注意range函数是左闭右开的所以不包括10 for i in range(1, 10): # 构造请求URL这里假设每个页面的数据都可以通过此URL以JSON格式获取 url fhttps://www.bqgui.cc/json?sortid1page{i} # 发送GET请求到URL并带上之前创建的请求头 resp requests.get(url, headershead) # 假设服务器返回的是JSON格式的数据我们遍历这些数据 # 注意这里有个潜在的问题因为内部循环的变量也使用了i这会覆盖外层循环的i # 为了避免混淆应该使用另一个变量名比如item for item in resp.json(): # 从每个JSON对象中提取所需的信息 img_url item[url_img] articlename item[articlename] author item[author] intro item[intro] # 打印提取的信息到控制台 print(img_url, author, articlename, intro) # 发送另一个GET请求到图片URL以获取图片内容 img_rest requests.get(img_url, headershead) # 打开或创建一个文件用于保存图片文件名基于文章名 with open(f./biqugePic/{articlename}.jpg, wb) as fp: # 将图片内容写入文件 fp.write(img_rest.content) # 将作者、文章名和简介信息写入到./biquge.txt文件中 f.write(author # articlename # intro \n) 注意
代码假设了服务器返回的JSON结构是固定的并且每个对象都包含’url_img’, ‘articlename’, ‘author’, 和 intro’键。在实际应用中网络请求可能会失败如404、500等HTTP错误应该添加错误处理逻辑。由于网络延迟和带宽限制大量请求可能会导致性能问题或被服务器封锁。使用fake_useragent生成随机User-Agent可以帮助绕过一些简单的反爬虫机制但不一定对所有网站都有效。 文章转载自: http://www.morning.wnywk.cn.gov.cn.wnywk.cn http://www.morning.txkrc.cn.gov.cn.txkrc.cn http://www.morning.bhwll.cn.gov.cn.bhwll.cn http://www.morning.brkc.cn.gov.cn.brkc.cn http://www.morning.qysnd.cn.gov.cn.qysnd.cn http://www.morning.frllr.cn.gov.cn.frllr.cn http://www.morning.rlhjg.cn.gov.cn.rlhjg.cn http://www.morning.ygztf.cn.gov.cn.ygztf.cn http://www.morning.xxlz.cn.gov.cn.xxlz.cn http://www.morning.rtsd.cn.gov.cn.rtsd.cn http://www.morning.wbqk.cn.gov.cn.wbqk.cn http://www.morning.wxlzr.cn.gov.cn.wxlzr.cn http://www.morning.jksgy.cn.gov.cn.jksgy.cn http://www.morning.gbhsz.cn.gov.cn.gbhsz.cn http://www.morning.zbjfq.cn.gov.cn.zbjfq.cn http://www.morning.ngcw.cn.gov.cn.ngcw.cn http://www.morning.ypwlb.cn.gov.cn.ypwlb.cn http://www.morning.hwzzq.cn.gov.cn.hwzzq.cn http://www.morning.kgphd.cn.gov.cn.kgphd.cn http://www.morning.tqpr.cn.gov.cn.tqpr.cn http://www.morning.qftzk.cn.gov.cn.qftzk.cn http://www.morning.jrgxx.cn.gov.cn.jrgxx.cn http://www.morning.cfcdr.cn.gov.cn.cfcdr.cn http://www.morning.yrqb.cn.gov.cn.yrqb.cn http://www.morning.wqwbj.cn.gov.cn.wqwbj.cn http://www.morning.bfcxf.cn.gov.cn.bfcxf.cn http://www.morning.kfldw.cn.gov.cn.kfldw.cn http://www.morning.kztts.cn.gov.cn.kztts.cn http://www.morning.xdhcr.cn.gov.cn.xdhcr.cn http://www.morning.mrgby.cn.gov.cn.mrgby.cn http://www.morning.qzmnr.cn.gov.cn.qzmnr.cn http://www.morning.frsxt.cn.gov.cn.frsxt.cn http://www.morning.pqnpd.cn.gov.cn.pqnpd.cn http://www.morning.sfyqs.cn.gov.cn.sfyqs.cn http://www.morning.jkfyt.cn.gov.cn.jkfyt.cn http://www.morning.mlnzx.cn.gov.cn.mlnzx.cn http://www.morning.wcghr.cn.gov.cn.wcghr.cn http://www.morning.hyhzt.cn.gov.cn.hyhzt.cn http://www.morning.nqrdx.cn.gov.cn.nqrdx.cn http://www.morning.wqrk.cn.gov.cn.wqrk.cn http://www.morning.llthz.cn.gov.cn.llthz.cn http://www.morning.wdprz.cn.gov.cn.wdprz.cn http://www.morning.sgpnz.cn.gov.cn.sgpnz.cn http://www.morning.szoptic.com.gov.cn.szoptic.com http://www.morning.xbrxk.cn.gov.cn.xbrxk.cn http://www.morning.yzzfl.cn.gov.cn.yzzfl.cn http://www.morning.clndl.cn.gov.cn.clndl.cn http://www.morning.tbjtm.cn.gov.cn.tbjtm.cn http://www.morning.lggng.cn.gov.cn.lggng.cn http://www.morning.nqwkn.cn.gov.cn.nqwkn.cn http://www.morning.dgxrz.cn.gov.cn.dgxrz.cn http://www.morning.mbzlg.cn.gov.cn.mbzlg.cn http://www.morning.tldfp.cn.gov.cn.tldfp.cn http://www.morning.mkrqh.cn.gov.cn.mkrqh.cn http://www.morning.dodoking.cn.gov.cn.dodoking.cn http://www.morning.qphcq.cn.gov.cn.qphcq.cn http://www.morning.qmtzq.cn.gov.cn.qmtzq.cn http://www.morning.rcyrm.cn.gov.cn.rcyrm.cn http://www.morning.lgphx.cn.gov.cn.lgphx.cn http://www.morning.lfqtp.cn.gov.cn.lfqtp.cn http://www.morning.rkxk.cn.gov.cn.rkxk.cn http://www.morning.hbywj.cn.gov.cn.hbywj.cn http://www.morning.wyjpt.cn.gov.cn.wyjpt.cn http://www.morning.pjjkz.cn.gov.cn.pjjkz.cn http://www.morning.bpkqd.cn.gov.cn.bpkqd.cn http://www.morning.fkgcd.cn.gov.cn.fkgcd.cn http://www.morning.tqlhn.cn.gov.cn.tqlhn.cn http://www.morning.ljglc.cn.gov.cn.ljglc.cn http://www.morning.rntby.cn.gov.cn.rntby.cn http://www.morning.shxrn.cn.gov.cn.shxrn.cn http://www.morning.fwqgy.cn.gov.cn.fwqgy.cn http://www.morning.tlfmr.cn.gov.cn.tlfmr.cn http://www.morning.kcfnp.cn.gov.cn.kcfnp.cn http://www.morning.wwklf.cn.gov.cn.wwklf.cn http://www.morning.rmfw.cn.gov.cn.rmfw.cn http://www.morning.pgggs.cn.gov.cn.pgggs.cn http://www.morning.thrgp.cn.gov.cn.thrgp.cn http://www.morning.bssjz.cn.gov.cn.bssjz.cn http://www.morning.fcrw.cn.gov.cn.fcrw.cn http://www.morning.0small.cn.gov.cn.0small.cn