English 中文(简体)
页: 1
原标题:Python 3 HTML parser

我确信每个人都会干,,并告诉我看一下文件(我有文件),但我不理解如何实现以下目标:

curl -s http://www.maxmind.com/app/locate_my_ip | awk  /align="center">/{getline;print} 

All I have in python3 so far is:

import urllib.request

f = urllib.request.urlopen( http://www.maxmind.com/app/locate_my_ip )

for lines in f.readlines():
    print(lines)

f.close()

严肃地说,任何建议(请不要把我带上。 当我学习了一天的 p,很容易被混淆的时候,一个简单的例子就是说!

最佳回答

以上是靠 la手的回答。

f = urllib.request.urlopen( http://www.maxmind.com/app/locate_my_ip )
for line in f:
    if b align="center">  in line:
        print(next(f).decode().rstrip())
f.close()

Explanation:

。 就像你在一份清单中的物品一样,在档案中,用手法的线上 it开。

if bcomp=“center”> in line look for the string line=“center”> in the present line. <>条码>b表示,这是tes的缓冲,而不是扼杀。 看来,urllib.reqquest.url open将结果作为双向数据而不是单编码加密,而未加注的<代码>趋同=“center”>将被解释为统一编码。 (上述<代码>TypeError的来源)。

<代码>next(f)在文档的下行,因为你的原 a在“电离层”和“电离层”之后印刷了该行。 <代码>decode 方法(载体内有办法)采用双亲数据,将其转换成可打印的单体编码物体。 <代码>rstrip()方法将任何线 trail的白色空间(即每个线末的新线)排除在外。

问题回答

我或许会利用定期表达方式,使议会本身:

import re
import urllib

f = urllib.request.urlopen( http://www.maxmind.com/app/locate_my_ip )
html_text=f.read()
re.findall(r d{1,3}.d{1,3}.d{1,3}.d{1,3} ,html_text)[0]

which will print the first string of the format: 1-3digits, period, 1-3digits,...

I take it you were looking for the line, you could simply extend the string in the findall() expression to take care of that. (see the python docs for re for more details). By the way, the r in front of the match string makes it a raw string so you wouldn t need to escape python escape characters inside of it (but you still need to escape RE escape characters).

帮助的希望





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签