【发布时间】:2014-05-07 22:48:53
【问题描述】:
我一直在尝试使用此代码来提取网址,但我无法获得以 html 显示的谷歌地图网址。当我尝试在此段中查找 url 时,它返回“无”。
import urllib
from bs4 import BeautifulSoup
from urllib.parse import urlparse
from urllib.request import urlopen
url="http://www.example.com"
html=urlopen(url)
soup=BeautifulSoup(html)
for tag in soup.findAll('a',href=True):
print(tag['href'])
<div class="map_container">
<div id="map_canvas" style="width: 100%; height: 450px; margin-top: 10px; position: relative; background-color: rgb(229, 227, 223); overflow: hidden; -webkit- transform: translateZ(0px);">
<div class="gm-style" style="position: absolute; left: 0px; top: 0px; overflow: hidden; width: 100%; height: 100%; z-index: 0;">
<div style="position: absolute; left: 0px; top: 0px; overflow: hidden; width: 100%; height: 100%; z-index: 0;">...</div>
<div style="margin-left: 5px; margin-right: 5px; z-index: 1000000; position: absolute; left: 0px; bottom: 0px;">
<a target="_blank" href="http://maps.google.com/mapsll=28.535959,77.146119&z=14&t=m&hl=en&gl=US&mapclient=apiv3" title="Click to see this area on Google Maps" style="position: static; overflow: visible; float: none; display: inline;">
<div style="width: 62px; height: 26px; cursor: pointer;">...</div>
</a>
</div>
</div>
</div>
</div>
【问题讨论】:
-
在呈现您尝试抓取的页面时可能需要 Javascript。在这种情况下,
urllib请求不会完全按照您在浏览器中看到的那样呈现该页面。为此,您需要使用Selenium。 -
将
soup=BeautifulSoup(html)更改为soup=BeautifulSoup(html, 'html.parser')有帮助吗? -
您是如何尝试查找标签属性的?它看起来就在我身边。
<a>标签,对吧? -
@alecxe 将 soup=BeautifulSoup(html) 更改为 soup=BeautifulSoup(html, 'html.parser') 没有帮助。
-
@aIKid 是的,我正在使用
<a>标签
标签: python html web-scraping html-parsing beautifulsoup