查看“Python爬虫案例:使用Selenium爬取中国制造网供应商”的源代码
←
Python爬虫案例:使用Selenium爬取中国制造网供应商
跳转至:
导航
,
搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
==代码== <nowiki> from selenium import webdriver from selenium.webdriver.common.keys import Keys import time # 使用前先导入By类 from selenium.webdriver.common.by import By from bs4 import BeautifulSoup driver = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe") #根据url模拟访问网站 def browse(url): try: driver.get(url) scroll() print("访问成功!") return "ok" except TimeoutException: return browse(url) def scroll():#模拟页面下拉滚动函数 for i in range(1,11): driver.execute_script("window.scrollTo(0,document.body.scrollHeight*"+str(i)+"/10)") #time.sleep(2) #得到供应商信息 def get_suppliers(): #wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#mainsrp-itemlist .items .item'))) soup = BeautifulSoup(driver.page_source, "html.parser") h2_list = soup.find_all('h2',class_ = 'company-name') for h in h2_list: print(h.get_text().strip()) def main(): start = time.clock() url =r'https://www.made-in-china.com/manufacturers-directory/item3/Machine-Tools-1.html' browse(url) get_suppliers() elapsed = time.clock()-start print("Time used:",elapsed) if __name__ == '__main__': main() </nowiki>
返回至
Python爬虫案例:使用Selenium爬取中国制造网供应商
。
导航菜单
个人工具
登录
命名空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
最近更改
随机页面
帮助
工具
链入页面
相关更改
特殊页面
页面信息