admin 管理员组文章数量: 893695
斗鱼房间信息自动化爬取
此处选择的网址是斗鱼绝地求生界面。使用自动化测试工具selenium,为什么选selenium而不要requests呢? 因为在翻页的时候网址不会变化,使用requests没有next_url不太方便。xpath路径最好自己写,灵活一点,F12选中元素copy xpath只能选中单个元素。
主要遇到了两个坑。一个是在加载页面和翻页的时候,要time.sleep()几秒。二是写“下一页”标签的xpath路径时,class属性中有空格,就算复制过来,空格也会自动消失,需要自己手动写。
import time
from pprint import pprint
from selenium import webdriverclass douyu(object):def __init__(self):self.start_url=''self.driver=webdriver.Chrome()def get_content_list(self):time.sleep(10)div_list=self.driver.find_elements_by_xpath('//div[@class="DyListCover-content"]')list=[]for i in div_list:dict = {}dict['anchor']=i.find_element_by_xpath('./div[2]/h2').textdict['people_num']=i.find_element_by_xpath("./div[2]/span").textdict['title']=i.find_element_by_xpath("./div[1]/h3").get_attribute('title')list.append(dict)pprint(list)def next_page(self):# 这里有个大坑!!class属性前面有空格self.to_next=self.driver.find_elements_by_xpath("//li[@class=' dy-Pagination-next']")return self.to_nextdef save_content(self):#保存数据自定义。上面打印出来了,不保存也没关系。passdef run(self):self.driver.maximize_window()self.driver.get(self.start_url)self.get_content_list()while self.next_page():self.to_next[0].click()time.sleep(5)self.get_content_list()self.driver.close()if __name__ == '__main__':dy=douyu()dy.run()
本文标签: 斗鱼房间信息自动化爬取
版权声明:本文标题:斗鱼房间信息自动化爬取 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1688189984h190003.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论