Python character encoding processing to find an IP location

Source: Internet
Author: User

Often go to Webmaster tools to find the IP of a domain name, and then locate the location of the IP, where, such as which province operators, Python can do a good job, mainly using the URLLIB2 and re modules

The following code is directly affixed:


Import Urllib2

Import re

Url= "ip.chinaz/? ip=211.152.42.6 "

Res=urllib2.urlopen (URL)

Msg=res.read ()

#此处一定要使用Unicode编码

Find_text=u "Query Results"

#因为网页内容为默认的utf-8 encoding, so it needs to be converted to utf-8 encoding to correctly match

Doc=re.search (' \<strong class= ' red ' \> ' +find_text.encode (' utf-8 ') + ' .*\</strong\> ', msg)

If Doc:

Result=doc.group ()

Print Result.decode (' Utf-8 ')

The results are as follows:

in [+]: Print Result.decode (' Utf-8 ')
<strong class= "Red" > Results [1]: 211.152.42.6 ==>> 3549964806 ==>> Shanghai Beijing century internet Broadband data center Telecom node </strong>


Using native Python is handy, of course, if you have beautifulsoup you can use it directly.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python character encoding processing to find an IP location

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.