日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python selenium对象怎么序列化_python selenium爬取斗鱼

發布時間:2025/3/8 python 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python selenium对象怎么序列化_python selenium爬取斗鱼 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

不加延遲報錯selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {“method”:”xpath”,”selector”:”.//span[@class=”DyListCover-hot”]”}

(Session info: chrome=80.0.3987.122)

最開始以為是版本問題,不過應該不會,我檢查了下版本

然后我注釋掉這一段

然后報Message: stale element reference: element is not attached to the page document

但是卻爬取到一段消息

說明有可能是延時的問題,在開頭加上延時,ok

附上源碼

import json

import time

from selenium import webdriver

driver=webdriver.Chrome()

driver.get("https://www.douyu.com/directory/all")

# driver.close()

#

def douyu():

##要加延遲,不然要報錯 time.sleep(5)

li_list=driver.find_elements_by_xpath('//*[@id="listAll"]/section[2]/div[2]/ul/li')

# print(list_all)

content_dict={}

for li in li_list:

content_dict["title"]=li.find_element_by_xpath(".//h3").text

content_dict["belong"]=li.find_element_by_xpath('.//span[@class="DyListCover-zone"]').text

content_dict["hot"]=li.find_element_by_xpath('.//span[@class="DyListCover-hot"]').text

content_dict["author"] = li.find_element_by_xpath(".//h2").text

print(content_dict)

#將字典轉換為字符串便于存儲

s=json.dumps(content_dict,ensure_ascii=False) #json序列化默認對中文采用ascii編碼,所以False

with open("douyu.txt","a",encoding="utf-8") as f:

f.write(s+'\n')

next_url=driver.find_elements_by_xpath('//li[@title="下一頁"]/span[@class="dy-Pagination-item-custom"]')

#三元表達式

next_url=next_url[0] if len(next_url) > 0 else None

while next_url is not None:

next_url.click()

time.sleep(3)

#我調我自己 點擊下一頁停5s繼續爬取 延時設在開頭 douyu()

douyu()

總結

以上是生活随笔為你收集整理的python selenium对象怎么序列化_python selenium爬取斗鱼的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。