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 regular expressions to extract site addresses from search results in Python

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

Share

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

Today, I will talk to you about how to use regular expressions to extract the address of the site in the search results in Python. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

This involves several issues that need to be addressed:

1. Get the text of the search result

To get more addresses, I used Google's advanced search function, which displays 100 results per page.

After you get the displayed results, you can view the source code and keep it in a text file to get the search result text.

2. Analyze how to extract site information

First of all, you need to analyze the acquired page to see how the site information can be extracted.

I use the profiler function in IE8's native development tools (press F12 to pop up) to see if there is any special format for what I care about.

From the image above, you can see that the site I need is in the tag, so can I just use regular expressions to extract the text?

3. Write a regular expression to get the site address

The next step is to write the expression. I wrote it in Python3.2, which is convenient and easy to use.

The code is as follows, first keep the search results page in e:/t3.txt, and then execute the following code

Import rep = re.compile (r'([^\ /]. +?)) f = open ("e:/t3.txt", encoding='utf-8') content = f.read () print ("\ n" .join (p.findall (content) after reading the above, do you have any further understanding of how to use regular expressions to extract site addresses from search results in Python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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