采购需求-列表页.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on 2023-7-22
  4. ---------
  5. @summary: 云采通高校采购联盟
  6. ---------
  7. @author: lzz
  8. """
  9. from collections import namedtuple
  10. import execjs
  11. import feapder
  12. from feapder.utils.tools import timestamp_to_date
  13. from items.spider_item import BidingListItem, DataBakItem
  14. from untils.attachment import AttachmentDownloader
  15. class Spider(feapder.BiddingListSpider):
  16. def start_callback(self):
  17. Menu = namedtuple('Menu', ['channel', 'code', 'crawl_page'])
  18. self.site = "云采通高校采购联盟"
  19. self.menus = [
  20. Menu('采购需求', 'a_yctgxcglm_cgxq', 2),
  21. ]
  22. def start_requests(self):
  23. url = "https://www.yuncaitong.cn/api/publish/solr/infoSearch"
  24. for menu in self.menus:
  25. yield feapder.Request(url, item=menu._asdict(), page=1)
  26. def download_midware(self, request):
  27. page = request.page
  28. params = {
  29. "page": f"{page-1}",
  30. "rows": "12",
  31. "timeBegin": "1682385621358",
  32. "infoType": "purchaseAnnouncement"
  33. }
  34. with open('./yctgxcglm_pm.js', 'r') as f:
  35. ex_js = f.read()
  36. ctx = execjs.compile(ex_js)
  37. pm = ctx.call('get_pm')
  38. request.headers = {
  39. "Accept": "application/json, text/plain, */*",
  40. "Accept-Language": "zh-CN,zh;q=0.9",
  41. "Authorization": "null",
  42. "Cache-Control": "no-cache",
  43. "Connection": "keep-alive",
  44. "Pragma": "no-cache",
  45. "Referer": "https://www.yuncaitong.cn/publish/demand.shtml",
  46. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
  47. "X-Requested-At": pm.get('At'),
  48. "X-Requested-Nonce": pm.get('Nonce'),
  49. "X-Requested-Token": pm.get('Token'),
  50. }
  51. request.params = params
  52. def parse(self, request, response):
  53. menu = request.item
  54. info_list = response.json
  55. for info in info_list:
  56. id_ = info.get('id')
  57. title = info.get('subject').strip()
  58. create_time = timestamp_to_date(int(str(info.get('createTime'))[:10]),time_format="%Y-%m-%d")
  59. href = f"https://www.yuncaitong.cn/publish/{create_time.replace('-','/')}/{id_}.shtml"
  60. area = "全国" # 省份
  61. city = "" # 城市
  62. data_item = DataBakItem() # 存储数据的管道
  63. data_item.href = href # 标书链接
  64. data_item.unique_key = ('title', 'href')
  65. data_item.channel = menu.get("channel") # 最上方定义的抓取栏目 (编辑器定的)
  66. data_item.spidercode = menu.get("code") # 最上方定义的爬虫code(编辑器定的)
  67. data_item.title = title # 标题
  68. data_item.site = self.site
  69. data_item.publishtime = create_time
  70. data_item.area = area # 城市默认:全国
  71. data_item.city = city # 城市 默认为空
  72. if info.get('contentType') == "PDF":
  73. file_url = href.replace('.shtml', '/content.pdf')
  74. attachments = {}
  75. attachment = AttachmentDownloader().fetch_attachment(
  76. file_name=title,
  77. file_type='pdf',
  78. download_url=file_url,
  79. proxies=request.get_proxies()
  80. )
  81. if attachment.get('size'):
  82. data_item.contenthtml = '详情请访问原网页!'
  83. attachments[str(len(attachments) + 1)] = attachment
  84. data_item.projectinfo = {"attachments": attachments}
  85. yield data_item
  86. else:
  87. list_item = BidingListItem()
  88. list_item.unique_key = ('href', 'title')
  89. list_item.parse = "self.detail_get" # 详情页回调方法
  90. list_item.deal_detail = ['//div[@class="project-details positionrl"]',
  91. '//div[@class="content"]'] # 抽取正文xpath
  92. list_item.proxies = False
  93. list_item.parse_url = href # 详情页请求地址
  94. list_item.href = href # 标书链接
  95. list_item.channel = menu.get("channel") # 最上方定义的抓取栏目 (编辑器定的)
  96. list_item.spidercode = menu.get("code") # 最上方定义的爬虫code(编辑器定的)
  97. list_item.title = title # 标题
  98. list_item.site = self.site
  99. list_item.publishtime = create_time
  100. list_item.area = area # 城市默认:全国
  101. list_item.city = city # 城市 默认为空
  102. yield list_item
  103. # 无限翻页
  104. request = self.infinite_pages(request, response)
  105. yield request
  106. if __name__ == "__main__":
  107. Spider(redis_key="lzz:yctgxcglm_bggg").start()