四虎国产精品永久地址998_chinesexxx少妇露脸_日本丁香久久综合国产精品_一区二区久久久久_四虎av影视_久久久久国产一区二区三区不卡

中培偉業(yè)IT資訊頻道
您現(xiàn)在的位置:首頁(yè) > IT資訊 > 軟件研發(fā) > 如何使用Python構(gòu)建打字機(jī)器人,以及進(jìn)行Web抓取

如何使用Python構(gòu)建打字機(jī)器人,以及進(jìn)行Web抓取

2020-08-27 17:46:37 | 來(lái)源:中培企業(yè)IT培訓(xùn)網(wǎng)

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

  一、如何使用Python構(gòu)建打字機(jī)器人

先決條件

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進(jìn)行Web抓取

僅需9行代碼即可使用Python進(jìn)行Web抓取。

先決條件

如果還沒(méi)有安裝,請(qǐng)安裝beautifulsoup4和請(qǐng)求。

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']

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

要檢查您是否正確執(zhí)行了該操作,數(shù)據(jù)輸出應(yīng)類似于以下內(nèi)容:

{'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抓取功能很棒,當(dāng)您想從網(wǎng)站中快速提取數(shù)據(jù)時(shí),可以節(jié)省大量時(shí)間。上面的示例可幫助您快速入門。

好了以上就是關(guān)于如何使用Python構(gòu)建打字機(jī)器人,以及進(jìn)行Web抓取的全部?jī)?nèi)容介紹,想了解更多關(guān)于Python的信息,請(qǐng)繼續(xù)關(guān)注中培偉業(yè)。

主站蜘蛛池模板: 湛江市| 铜陵市| 罗平县| 临武县| 博客| 铜鼓县| 平舆县| 阜阳市| 寻乌县| 丰镇市| 玛纳斯县| 连江县| 长海县| 汶川县| 乌海市| 汉阴县| 黄石市| 汉阴县| 彭阳县| 陵川县| 茌平县| 板桥市| 南开区| 宜良县| 新营市| 邻水| 濮阳市| 浦北县| 泌阳县| 松原市| 清苑县| 巴青县| 秦皇岛市| 巨鹿县| 四川省| 澄江县| 泌阳县| 佛坪县| 班戈县| 松原市| 呈贡县|