Bladeren bron

fix: 删除添加的 cookie,优化获取文章逻辑

wuwenyi 1 maand geleden
bovenliggende
commit
d02ae696ba
1 gewijzigde bestanden met toevoegingen van 12 en 7 verwijderingen
  1. 12 7
      xhs/__init__.py

+ 12 - 7
xhs/__init__.py

@@ -34,12 +34,13 @@ class XhsBrowser(BaseBrowser):
         # self.xhs_client = self.create_xhs_client(None)
         # self.page.goto('https://www.xiaohongshu.com/explore/66b4c36b000000001e01cf8a',wait_until='domcontentloaded')
         self.rotate_ident.handle_rotate()
-        self.browser.add_cookies([{
-            'name': "webId",
-            'value': "xxx123",  # any value
-            'domain': ".xiaohongshu.com",
-            'path': "/"
-        }])
+        # 这个 cookie是直接用 client时候用的,如果用 playwright获取数据,不要打开
+        # self.browser.add_cookies([{
+        #     'name': "webId",
+        #     'value': "xxx123",  # any value
+        #     'domain': ".xiaohongshu.com",
+        #     'path': "/"
+        # }])
 
     # def create_xhs_client(self, httpx_proxy: Optional[str]) -> XiaoHongShuClient:
     #     """Create xhs client"""
@@ -89,7 +90,11 @@ class XhsBrowser(BaseBrowser):
 
     def get_note(self, note_id: str):
         # note = self.xhs_client.get_note_by_id(note_id=note_id)
-        self.page.goto(f'{XHS_URL}/explore/{note_id}', wait_until='domcontentloaded')
+        url = f'{XHS_URL}/explore/{note_id}'
+        self.page.goto(url)
+        if self.page.url != url:
+            self.page.locator('.note-item').nth(0).click()
+            self.page.goto(url)
         self.rotate_ident.handle_rotate()
         data = self.page.evaluate('noteId => window.__INITIAL_STATE__ && JSON.stringify(window.__INITIAL_STATE__.note.noteDetailMap[noteId].note)', note_id)
         return json.loads(data)