Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use selenium to crawl memes

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to use selenium to climb emojis", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use selenium to crawl emojis.

Https://www.doutula.com/photo/list/?page=1

However, this site can not request content directly from requests, so it should also be loaded by js. So the easiest way is to use selenium, omnipotent!

The structure of the website

If it is gif, there will be an extra img tag.

Write out the xpath according to this, take out the address of the picture, and download it using urllib

Save after the download is complete. Then send it to friends or groups through the wxpy library.

Specific code

Use selenium to get emojis and save them to a file. Be careful to save the file name in English or a number, otherwise when you use the wxpy library to simulate sending, you will directly report an error.

Def get_doutu ():

Browser = webdriver.Chrome ()

# img_url_dic = {}

For i in range (1,2):

Browser.get ('https://www.doutula.com/photo/list/?page=%s'% str (I))

Time.sleep (4)

Eles = browser.find_elements_by_xpath ('/ / div [@ class= "container_"] / div [@ id= "pic-detail"] / div [@ class= "row"] / div [@ class= "col-sm-9"] / div [@ class= "random_picture"] / ul [@ class= "list-group"] / li [@ class= "list-group-item"] / div [@ class= "page-content text-center"] / div/a [@ class= "col-xs-6 col -sm-3 "] / img [@ data-original]')

# names = browser.find_elements_by_xpath ('/ / div [@ class= "container_"] / div [@ id= "pic-detail"] / div [@ class= "row"] / div [@ class= "col-sm-9"] / div [@ class= "random_picture"] / ul [@ class= "list-group"] / li [@ class= "list-group-item"] / div [@ class= "" page-content text-center "] / div/a [@ class=" col-xs-6 Col-sm-3 "] / img [@ data-original]')

For j in range (len (eles)):

Url = els [j]. Get _ attribute ('data-original')

Url = url.split ('!') [0]

Ext = url.split ('.') [- 1]

Name = random.randrange (1, 10000, 2)

Filename = str (name) +'.'+ ext

# Save image data

Data = urllib.request.urlopen (url). Read ()

F = open ('image/' + filename,' wb')

F.write (data)

F.close ()

Time.sleep (5)

Browser.close ()

Browser.quit ()

Get the list of files, print to recharge, concatenate the address of the file, and send it to send_news (), which sends it.

Def listdir (file_dir):

For root, dirs, files in os.walk (file_dir):

Print (root) # current directory path

Print (dirs) # all subdirectories under the current path

Print (files) # all non-directory subfiles under the current path

Print (type (files))

For i in files:

Send_news (file_dir + I)

Time.sleep (10)

Log in to the wxpy package, log in to Wechat and send

Def login_wechat ():

Global bot

Bot = Bot ()

# bot = Bot (console_qr=2,cache_path= "botoo.pkl") # used on linux environment

Def send_news (data):

If bot = = None:

Login_wechat ()

Try:

My_group = bot.groups () .search (u 'tease four') [0]

Time.sleep (20)

Print (data)

My_group.send_image (data)

Except Exception as e:

Print (u "failed!!" , e)

It's best to set the interval. Be careful of being cleaned up.

At this point, I believe you have a deeper understanding of "how to use selenium to crawl emojis". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report