lizongze преди 2 години
родител
ревизия
9adae69a0a
променени са 1 файла, в които са добавени 19 реда и са изтрити 3 реда
  1. 19 3
      FworkSpider/feapder/templates/spider_template.tmpl

+ 19 - 3
FworkSpider/feapder/templates/spider_template.tmpl

@@ -45,7 +45,7 @@ class ${spider_name}(feapder.BiddingListSpider):
         driver = response.browser
         menu = request.item
         info_list = response.xpath('')
-        for info in info_list:
+        for index,info in enumerate(info_list):
             # href = info.xpath('').extract_first().strip()
             title = info.xpath('').extract_first().strip()
             publish_time = info.xpath('').extract_first().strip()
@@ -54,9 +54,21 @@ class ${spider_name}(feapder.BiddingListSpider):
             city = ""      # 城市
             district = ""  # 区县
 
-            next_page = driver.find_element_by_xpath(f'//a[contains(text(),"{title}")]')
-            next_page.click()
+            try:
+                next_page = driver.find_elements_by_xpath(f'//a[contains(text(),"{title}")]')[index]  # index防止标题相同
+            except:
+                try:
+                    next_page = driver.find_elements_by_xpath(f'//a[contains(text(),"{title[:10]}")]')[index] # 标题过长
+                except:
+                    continue
+
+            driver.execute_script("arguments[0].click();",next_page) # js点击
             time.sleep(3)
+
+            # 点击三级页标题后打开新窗口
+            # handles = driver.window_handles
+            # driver.switch_to.window(handles[-1])
+
             href = driver.current_url
 
             data_item = DataBakItem()                # 存储数据的管道
@@ -81,6 +93,10 @@ class ${spider_name}(feapder.BiddingListSpider):
 
             data_item.contenthtml = html
 
+            # (不同窗口)切换回主窗口
+            # driver.close()
+            # driver.switch_to.window(handles[0])
+
             driver.back()
             time.sleep(3)