网站建设计划书,大智慧手机版官方下载,宁夏电建网站,制作网站教程工具库
爬虫有两种方案#xff1a;
第一种方式是使用request模拟请求#xff0c;并使用bs4解析respond得到数据。第二种是使用selenium和无头浏览器#xff0c;selenium自动化操作无头浏览器#xff0c;由无头浏览器实现请求#xff0c;对得到的数据进行解析。
第一种方…
工具库
爬虫有两种方案
第一种方式是使用request模拟请求并使用bs4解析respond得到数据。第二种是使用selenium和无头浏览器selenium自动化操作无头浏览器由无头浏览器实现请求对得到的数据进行解析。
第一种方案部署简单效率高对于静态页面效果较好对于动态页面效果较差。【可以理解为直接与服务器对接申请什么数据完全由你自己来决定】 对于网页来说可以分为静态网页和动态网页二者的区别是静态网页是对于你的申请切切实实存在一个html网页文件将这个文件发给你你浏览器进行渲染。而动态网页则是存在一个服务器框架处理你的请求临时组合成一个html网页发给你你浏览器进行渲染你得到的是服务器框架的产物。 因此网页的数据来源也可以分为 1、静态网页内的 2、通过Ajax接口申请的例如商品的评价数量加载网页时不随网页一块儿得到而是额外申请 3、通过JS脚本运行Ajax接口申请的例如商品的具体评价只有你点评论栏JS脚本才会向服务器申请数据 第二种方案部署稍微麻烦需要安装无头浏览器但是爬取效果较好因为是真实的浏览器申请selenium是模拟真人进行操作对于反爬虫效果较好。 本文使用的是第一种所需的工具库 Python库 Beautifulsoup request json
方法
1、登录京东获取登录cookie 2、搜索得到搜索链接 3、使用request向搜索链接发送请求得到respond 4、使用bs4解析respond 5、定位想要的数据所在的tag 6、对于一些动态数据在浏览器开发者工具的network中找到相应的服务器地址使用request模拟请求并使用json解析服务器的respond
代码
import requests, json
from bs4 import BeautifulSoup# 基类后续可以在此之上扩展
class AbstractWebPage:def __init__(self, cookie, use_cookieTrue):if use_cookie:self.headers {User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36,cookie: cookie}else:self.headers {User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36}self.sess requests.session()self.sess.headers.update(self.headers)# 目录类用来表示搜索结果
class Content(AbstractWebPage):def __init__(self, cookie, keyword, end_page):super(Content, self).__init__(cookie)start_url https://search.jd.com/Search?keyword keyword encutf-8wq keywordself.url_list [start_url page str(j) for j in range(1, end_page 1)]self.end_page end_pagedef print(self):print(self.url_list, sep\n)def get_item_info(self):item_pages_list []with open(good_info.txt, w, encodingutf-8) as f:f.write(产品名称 \t 价格 \t 销量 \t 店铺 \n)f.write(* * 50 \n)for url in self.url_list:res self.sess.get(url)res.encoding utf-8res res.text# 定位搜索结果主体并获取所有的商品的标签soup BeautifulSoup(res, html.parser).select(#J_goodsList ul)good_list soup[0].select([classgl-i-wrap])# 循环获取所有商品信息for temp in good_list:# 获取名称信息name_div temp.select_one([classp-name p-name-type-2])good_info name_div.text.strip() \t# 价格信息price_div temp.select_one([classp-price])good_info price_div.text.strip() \t# 评价信息comment_div temp.select_one([classp-commit]).find(strong).find(a)comment_url comment_div.get(href)good_id comment_url.replace(//item.jd.com/, ).replace(.html#comment, )# 评价信息没有在主页面内而是需要另外发送GET获取服务器地址如下# 这里面的uuid是唯一标识符如果运行程序发现报错或者没有得到想要的结果# commit_start_url fhttps://api.m.jd.com/?appiditem-v3functionId \# pc_club_productCommentSummariesclientpcclientVersion1.0.0t \# f1711091114924referenceIds{good_id}categoryIds9987%2C653%2C655 \# loginType3bbtfshielduuid181111935.1679801589641754328424.1679801589 \# .1711082862.1711087044.29commit_start_url fhttps://api.m.jd.com/?appiditem-v3functionId \pc_club_productCommentSummariesclientpcclientVersion1.0.0t \f1711091114924referenceIds{good_id}categoryIds9987%2C653%2C655# 发送请求得到结果comment_res self.sess.get(commit_start_url)# 编码方式是GBK国标编码comment_res.encoding gbkcomment_res_json comment_res.json()# 解析得到评论数量good_info comment_res_json[CommentsCount][0][CommentCountStr] \t# 店铺信息shop_div temp.select_one([classp-shop])good_info shop_div.get_text().strip() \tf.write(good_info \n)f.write(* * 50 \n)f.close()return item_pages_listif __name__ __main__:# cookie用于验证登录状态必须要有cookie否则京东会提示网络繁忙请重试# 获取方法使用浏览器登录过后按F12点击弹出界面中最上方的network选项name栏里面随便点开一个拉到最下面就有cookie复制到cookie.txt中# 注意不要换行前后不要有空格只需要复制cookie的值不需要复制“cookie”这几个字符# 上面的看不懂的话看这个https://blog.csdn.net/qq_46047971/article/details/121694916# 然后就可以运行程序了cookie_str with open(cookie.txt) as f:cookie_str f.readline()# 输入cookie关键词输入结束页数content_page Content(cookie_str, 手机, 2)content_page.print()urls content_page.get_item_info()