2017-10-07 53 views
-1

我正在制作游戏,某种口袋妖怪可以去黑客攻击。 我的游戏必须下载设备的位置,但有一个问题。 我发现显示您的经纬度的网站,但我无法得到它。我制作了一个脚本:在Python中获取html网站的一部分

import urllib.request 
response = urllib.request.urlopen('http://python.org/') 
html = response.read() 
print(html) 

该脚本使站点代码分配给名为html的变量并将其打印出来。

(网站看起来好像是这样)

<!DOCTYPE html> 
<html lang="en"> 
<head>...</head> 
<body style> 
<div class="container"> 
    ::before 
    <div class="rows"> 
    <div class="col-md-4 current-location-detail"> 
     <table class="table"> 
     <trbody> 
      <tr> 
      <td width="30%">Latitude </td> 
      <td id="latitude">50.19869</td> 
      </tr> 
      <tr> 
      <td>Longitude </td> 
      <td id="longitude">17.17034</td> 
      </tr> 

我怎样才能得到经度和纬度使用此代码?请帮帮我。

+1

你是什么意思的“网站的一部分”?你想阅读的HTML没有完全下载它? (这是'urlopen'的作用),还是你想从页面中提取一些数据? – Anonta

+0

我需要纬度 –

+0

50.19869 –

回答

1

您可以BeautifulSoup尝试:

from urllib2 import urlopen 
from bs4 import BeautifulSoup 

response = urlopen('http://python.org/').read() 

# Create a BeautifulSoup object 
soup = BeautifulSoup(response, 'html.parser') 
soup.find("div", {"id": "success-story-2"}) 

在下面的例子中,我们收到的内容和元素ID“成功 - 故事2”。您可以使用find()find_all()来检索您希望的文档的各个部分。

+0

我可以在哪里下载这些库? –

+0

@PatrykPopowicz你不需要明确地安装urllib2 ...它已经在那里了......但对于BS4你可以做点安装beautifulsoup4 –