招标公告-列表页.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on 2025-04-24
  4. ---------
  5. @summary: TCM供应链平台
  6. ---------
  7. @author: lzz
  8. """
  9. import feapder
  10. from items.spider_item import MgpListItem
  11. from collections import namedtuple
  12. class Feapder(feapder.BiddingListSpider):
  13. def start_callback(self):
  14. Menu = namedtuple('Menu', ['channel', 'code', 'crawl_page'])
  15. self.site = "TCM供应链平台"
  16. self.menus = [
  17. Menu('招标公告', 'a_tclgylpt_zbgg', 1),
  18. ]
  19. self.VIEWSTATE = ""
  20. self.cookies = {}
  21. self.EVENTVALIDATION = ""
  22. self.VIEWSTATEENCRYPTED = ""
  23. def start_requests(self):
  24. for menu in self.menus:
  25. start_url = "https://tcm.cxtc.com/site/BiddingList.aspx"
  26. yield feapder.Request(url=start_url, item=menu._asdict(), page=1, proxies=False)
  27. def download_midware(self, request):
  28. request.headers = {
  29. "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
  30. "Accept-Language": "zh-CN,zh;q=0.9",
  31. "Cache-Control": "no-cache",
  32. "Connection": "keep-alive",
  33. "Content-Type": "application/x-www-form-urlencoded",
  34. "Origin": "https://tcm.cxtc.com",
  35. "Pragma": "no-cache",
  36. "Referer": "https://tcm.cxtc.com/site/BiddingList.aspx",
  37. "Upgrade-Insecure-Requests": "1",
  38. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36",
  39. }
  40. page = request.page
  41. if page != 1:
  42. data = {
  43. "__EVENTTARGET": "ctl00$MainContent$gv1",
  44. "__EVENTARGUMENT": f"Page${page}",
  45. "__VIEWSTATE": self.VIEWSTATE,
  46. "__VIEWSTATEENCRYPTED": self.VIEWSTATEENCRYPTED,
  47. "__EVENTVALIDATION": self.EVENTVALIDATION,
  48. "ctl00%24MainContent%24SearchTxt": "",
  49. "ctl00%24MainContent%24SearchTxt2": ""
  50. }
  51. request.data = data
  52. request.cookies = self.cookies
  53. def parse(self, request, response):
  54. self.cookies = response.cookies.get_dict()
  55. self.VIEWSTATE = response.xpath('//input[@id="__VIEWSTATE"]/@value').extract_first("")
  56. self.EVENTVALIDATION = response.xpath('//input[@id="__EVENTVALIDATION"]/@value').extract_first("")
  57. self.VIEWSTATEENCRYPTED = response.xpath('//input[@id="__VIEWSTATEENCRYPTED"]/@value').extract_first("")
  58. menu = request.item
  59. info_list = response.xpath('//table[@id="MainContent_gv1"]/tr')
  60. for info in info_list[1:-1]:
  61. href = info.xpath('./td[1]/div/a/@href').extract_first()
  62. title1 = info.xpath('./td[1]/div/a/text()').extract_first("")
  63. title = title1 + info.xpath('./td[2]/text()').extract_first().strip()
  64. create_time = info.xpath('./td[3]/text()').extract_first().strip().replace('/','-')
  65. area = "全国" # 省份
  66. city = "" # 城市
  67. list_item = MgpListItem() # 存储数据的管道
  68. list_item.href = href # 标书链接
  69. list_item.unique_key = ('title', 'href', create_time)
  70. list_item.channel = menu.get("channel") # 最上方定义的抓取栏目 (编辑器定的)
  71. list_item.spidercode = menu.get("code") # 最上方定义的爬虫code(编辑器定的)
  72. list_item.title = title # 标题
  73. list_item.site = self.site
  74. list_item.publishtime = create_time
  75. list_item.area = area # 城市默认:全国
  76. list_item.city = city # 城市 默认为空
  77. list_item.parse = "self.detail_get" # 详情页回调方法
  78. list_item.deal_detail = [] # 抽取正文xpath
  79. list_item.proxies = False
  80. list_item.parse_url = href # 详情页请求地址
  81. yield list_item
  82. # 无限翻页
  83. request = self.infinite_pages(request, response)
  84. yield request
  85. if __name__ == "__main__":
  86. Feapder(redis_key="lzz:tclgylpt_zbgg").start()