五月天堂网_免费看影片_1024手机看毛片_亚洲综合四区_男人的天堂影院_在线亚洲自拍

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

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

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

眾所周知,Python作為編程語言的一種,不僅語法簡單、易學(xué)并且很有前途,很多人都對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抓取。

先決條件

如果還沒有安裝,請安裝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']

這里是完整的代碼段,您可以將其直接復(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抓取的全部內(nèi)容介紹,想了解更多關(guān)于Python的信息,請繼續(xù)關(guān)注中培偉業(yè)。

主站蜘蛛池模板: 蜜桃精品在线 | 欧美美女被日 | 四虎成人精品一区二区免费网站 | 97精品久久 | 无码精品久久久天天影视 | 免费特黄视频 | 日本后进式猛烈xx00动态图 | 国产精品一区二区三区四区不卡 | 人妻少妇伦在线无码 | 99久久精品久久久久久清纯 | 2024AV天堂手机在线观看 | 免费看的一级视频 | 天堂网在线WWW最新版资源 | 特黄特色大片免费视频大全 | 国产美女高潮流白浆免费观看 | 欧美xxxxxxxxxxxx| 我爱52av好色 | 热久久久久久久 | 老子影院午夜伦手机不卡国产 | 国产在线观看99 | 日韩黄色大片在线观看 | chinese国产打屁股实践3 | 一本一生久久a久久精品综合蜜 | 色综合av综合无码综合网站 | 日日草日日干 | 狠狠操 狠狠干 | 叶璇三级露全乳电影 | 日本乱人片一区二区三区 | 一区二区三区免费 | 最新免费在线观看网址 | 蜜臀精品无码AV在线播放 | 五月婷婷干 | 久久98 | www.污在线观看 | 黄色在线观看www | 亚洲不卡一区二区三区 | 国产精品一区毛片 | 久久久久久久久久久久久久电影 | 午夜DV内射一区区 | 在线观看黄网站 | 中文字幕四区 |