
在来自美国500家最大公司的15,000多个Instagram帖子上使用网络抓取,降维和无监督的机器学习,以尝试并构成普通公司帖子的样式。
回到过去,当我曾经帮助计划和管理大学活动时,每天都会出现“样式指南”一词。想法是您发布的所有内容都应具有一致的字体,大小,调色板等。我认为这是一个有趣的练习,以查看哪些行业的图像帖子(或标准样式指南)最丰富,文字说明最丰富从Instagram的。
某些行业应该是相当标准的,尤其是面向消费者的行业(例如,汽车应该是一堆汽车)。其他行业可能根本就不是标准行业(例如财务咨询)!
我分三个步骤进行了操作:
获取每个公司的帖子的图像和文本数据关于图像和文本数据的PCA和t-SNE
高斯混合模型的异常检测
获取每个公司的帖子的图像和文本数据
事实证明,此部分比实际困难的过程耗时更多,这主要是由于所需的刮削量和超时所致。每个步骤的旁边都显示了每个脚本的运行时。要执行这些步骤,同时使用selenium和beautifulsoup4。第一步的脚本并不难,只是打开了一个使用它的webdriver并不断搜索name-of-company + ‘ instagram’,然后获取其中包含的第三个文本元素(@。首先导入以下软件包:
import pandas as pdimport numpy as npimport datetimefrom selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support.ui import WebDriverWait
然后,代码如下所示:
myurl = (“https://google.com”).replace(” “, “%20”)li = []driver = webdriver.Firefox()df = pd.read_excel(r’C:UsersAndrewDocumentsPythonScriptsNLPwebscrapinginstagram_full_S&P.xlsx’)names = df[“Security”]for name in names:driver.get(myurl) driver.find_element_by_xpath(‘/html/body/div/div[3]/form/div[2]/div[1]/div[1]/div/div[2]/input’).send_keys(name+” instagram”) driver.find_element_by_xpath(‘/html/body/div/div[3]/form/div[2]/div[1]/div[1]/div/div[2]/input’).send_keys(Keys.ENTER) time.sleep(1) username = driver.find_elements_by_xpath(“//*[contains(text(), ‘@’)]”) print(username[3].text) li.append(username[3].text)driver.quit()df = pd.Series( (v for v in li) )df.to_excel(r’C:UsersAndrewDocumentsPythonScriptsNLPwebscrapinginstagram_full_S&P_amended.xlsx’)
xpath检查页面上的元素后,可以右键单击html,然后转到copy → xpath。有时最好使用CSS selector,因为xpath不同的职位会有所不同。
为了抓取帖子,我将遍历以下内容:
driver.get(“instagram.com/” + username)滚动三下再拉 driver.page_source使用bs4将每张卡中的发布链接拉入数据框如果您向下滚动太远,Instagram似乎会从html卸载帖子链接,您可以拉出html并以某种方式附加它-但我觉得16,711篇帖子就足够了。最后一步是遍历每个发布链接,并提取图像src和发布文本。为了预防起见,我将图像保存到了桌面上,但是您无需这样做。针对不同的情况进行了一些测试(不同的帖子类型,没有帖子,旧格式等),但这并没有占用太多时间。我让它运行一整夜,然后将其保存在excel文件中。
我使用以下功能将照片读入扁平阵列:
defPC_to_image_flat(path):image = cv2.imread(path) image = image_resize(image, height=400, width=400)#will have to flatten it image = image.reshape(-1) #,3) # return the image return image
保存完所有照片后,我使用以下代码并行读取数组值(这里sorted_files是我所有以索引命名的图像文件的路径):
print(“reading in photos”)from multiprocessing import Pooldef read_photos(df):df[“Photo”] = df[0].apply(lambda x: PC_to_image_flat(x))return dfdef parallelize_dataframe(df, func, n_cores=4):df_split = np.array_split(df, n_cores)pool = Pool(n_cores)df = pd.concat(pool.map(func, df_split))pool.close()pool.join()returndfsorted_files = parallelize_dataframe(sorted_files,read_photos)full[“Photo”] = sorted_files[“Photo”]
尽管连接步骤所花费的时间超过了并行化应用程序所节省的时间,但我还是决定只使用swifter(它根据任务选择对Dask并行化进行矢量化)。运行大约需要4分钟。
import swiftersorted_files[“Photo”] = sorted_files[0].swifter.apply(lambda x: PC_to_image_flat(x))
如果您有磁盘空间,请使用此功能将它们保存在字节数组中:
defstore_many_hdf5(images, labels):””” Stores an array of images to HDF5.Parameters: ————— images images array, (N, 32, 32, 3) to be stored labels labels array, (N, 1) to be stored “”” num_images = len(images)# Create a new HDF5 file file = h5py.File(r’C:UsersAndrewDocumentsPythonScriptsNLPwebscrapingimages instagram’ / f”{num_images}_instagram.h5″, “w”)# Create a dataset in the file dataset = file.create_dataset( “images”, np.shape(images), h5py.h5t.STD_U8BE, data=images ) meta_set = file.create_dataset( “meta”, np.shape(labels), h5py.h5t.STD_U8BE, data=labels ) file.close()store_many_hdf5(corpus[“Photo”],corpus[“Link”]
跨境卖家的生意正在加速回暖。 LEKSHOP获悉,根据速卖通最新的一份调研报告显示,近8成卖家认为2023年中国整体跨境出口形势稳中向好。中国的跨境电商进出口规模5年来增长了近10倍,凭借线上交易、交易链条短等优势,跨境电商倍受外贸企业青睐。 “我们年前备货了…
码刀科技(www.lekshop.cn)是国内知名企业级电商平台提供商,为企业级商家提供最佳的电商平台搭建(多种模式电商平台搭建:B2B/B2B2C/B2C/O2O/新零售/跨境等)、平台管理系统开发及互联网采购解决方案服务, 联系客服了解更多.