Free性欧美Hd另类_精品亚洲欧美视频在线观看_freesex欧美喷水_字幕av在线_久久久久久国产免费_伊人5566

中培偉業IT資訊頻道
您現在的位置:首頁 > IT資訊 > 軟件研發 > 如何使用Python構建打字機器人,以及進行Web抓取

如何使用Python構建打字機器人,以及進行Web抓取

2020-08-27 17:46:37 | 來源:中培企業IT培訓網

眾所周知,Python作為編程語言的一種,不僅語法簡單、易學并且很有前途,很多人都對Python感興趣。Python的應用十分廣泛。經常關注我的朋友可能知道,關于Python的使用,之前已經為大家介紹了很多用法,今天主要介紹兩個方面的內容,一個是如何使用Python構建打字機器人,另一是如何使用Python進行Web抓取的內容。

  一、如何使用Python構建打字機器人

先決條件

pip install selenium

代碼段

# Importing required modules and initializing variablesfrom selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsimport re

string = ''

# ______________________________________

# Opening thetypingcat.com on firefox

firefox = webdriver.Firefox()

firefox.get

# ______________________________________

# Using javascript to get the typing content from the website and storing value in "string" variablefor i in range(firefox.execute_script('return document.querySelectorAll(".line").length')):

string += firefox.execute_script('return document.querySelectorAll(".line")['+str(i)+'].innerHTML')

string = re.sub(r'<[^>]*>','',string) #This line is just delete tags present inside string

# ______________________________________

# Selenium commands to type what is stored inside string variable on the focused screen

action = ActionChains(firefox)

action.send_keys(string)

action.perform()

# ______________________________________ END ______________________________________

  二、如何使用Python進行Web抓取

僅需9行代碼即可使用Python進行Web抓取。

先決條件

如果還沒有安裝,請安裝beautifulsoup4和請求。

pip install beautifulsoup4

pip install requests

代碼段

import requestsfrom bs4 import BeautifulSoup

result = requests.get

soup = BeautifulSoup(result.content)

links = soup.find_all("a", "title")

data = {}for link in links:

title = link.string

data[title] = link.attrs['href']

這里是完整的代碼段,您可以將其直接復制并粘貼到終端,收藏的文本編輯器或jupyter筆記本中。

要檢查您是否正確執行了該操作,數據輸出應類似于以下內容:

{'MSI GL62VR 7RFX': '/test-sites/e-commerce/allinone/product/326',

'Dell Vostro 15…': '/test-sites/e-commerce/allinone/product/283',

'Dell Inspiron 17…': '/test-sites/e-commerce/allinone/product/296'}

Web抓取功能很棒,當您想從網站中快速提取數據時,可以節省大量時間。上面的示例可幫助您快速入門。

好了以上就是關于如何使用Python構建打字機器人,以及進行Web抓取的全部內容介紹,想了解更多關于Python的信息,請繼續關注中培偉業。

主站蜘蛛池模板: 国产经典久久久 | 亚洲大香线伊人婷婷五月 | 欧美大片首页欧美大片首页 | 9999国产精品欧美久久久久久 | 男生戳美女两腿中间那个视频 | 老司机aⅴ在线精品导航 | 精品亚洲国产成人av不卡 | 2018天天躁夜夜躁狠狠躁 | 久久午夜精品福利一区二区 | 风间由美一区二区三区在线观看 | 久久99精品久久久秒播软件优势 | 先锋影音最新色资源站 | 高黄暴H日本在线观看 | 美女黄频 | av久热| 老司机狠狠干 | 麻豆蜜桃视频 | 日韩系列在线 | 国产精品乱码一区二三区 | 久久精品不卡一区二区 | yp最新网站入口 | 日本不卡视频一区二区三区 | 国产精品激情综合五月天中文字幕 | 一区二区三区在线播放视频 | 黄色视频网站在线观看免费 | 亚洲精品在线视频观看 | 成人A级视频在线播放 | 精品一区在线免费观看 | 久久久久久久国产毛片 | 午夜av影院| 成人在线观看日韩 | 久久久噜噜噜久久久 | av在线免费播放 | 无码人妻精品一区二区三区不卡 | 亚洲av久久久噜噜噜噜 | 99久久精品费精品国产一区二 | 免费无人区男男码卡二卡 | 抖音奶片无罩子52秒回放 | 欧洲最大免费看影视网站 | 国产亚洲精品久久飘花 | 精品成人av |