工具库
爬虫有两种方案:
- 第一种方式是使用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_cookie=True):
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 + '&enc=utf-8&wq=' + keyword
self.url_list = [start_url + '&page=' + str(j) for j in range(1, end_page + 1)]
self.end_page = end_page
def print(self):
print(self.url_list, sep='\n')
def get_item_info(self):
item_pages_list = []
with open("good_info.txt", 'w', encoding='utf-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-8'
res = res.text
soup = BeautifulSoup(res, 'html.parser').select('#J_goodsList > ul')
good_list = soup[0].select('[class=gl-i-wrap]')
for temp in good_list:
name_div = temp.select_one('[class="p-name p-name-type-2"]')
good_info = name_div.text.strip() + '\t'
price_div = temp.select_one('[class=p-price]')
good_info += price_div.text.strip() + '\t'
comment_div = temp.select_one('[class=p-commit]').find('strong').find('a')
comment_url = comment_div.get('href')
good_id = comment_url.replace('//item.jd.com/', '').replace('.html#comment', '')
commit_start_url = f'https://api.m.jd.com/?appid=item-v3&functionId' \
'=pc_club_productCommentSummaries&client=pc&clientVersion=1.0.0&t' \
f'=1711091114924&referenceIds={good_id}&categoryIds=9987%2C653%2C655'
comment_res = self.sess.get(commit_start_url)
comment_res.encoding = 'gbk'
comment_res_json = comment_res.json()
good_info += comment_res_json['CommentsCount'][0]['CommentCountStr'] + '\t'
shop_div = temp.select_one('[class=p-shop]')
good_info += shop_div.get_text().strip() + '\t'
f.write(good_info + '\n')
f.write("*" * 50 + '\n')
f.close()
return item_pages_list
if __name__ == "__main__":
cookie_str = ''
with open('cookie.txt') as f:
cookie_str = f.readline()
content_page = Content(cookie_str, '手机', 2)
content_page.print()
urls = content_page.get_item_info()