Python中刮刀的开始

Python中刮刀的开始,python,web-scraping,Python,Web Scraping,我在自学Python,通过做而不仅仅是阅读。 当我运行这个代码时,终端并没有显示错误,但什么也没有发生,尽管在我看来这应该是从这个列表中检索地址 我怎么称呼这些数据有什么问题吗 import requests from bs4 import BeautifulSoup url = ("http://www.gym-directory.com/listing/bulldog-gym/") r = requests.get(url) soup = BeautifulSoup(r.content,

我在自学Python,通过做而不仅仅是阅读。 当我运行这个代码时,终端并没有显示错误,但什么也没有发生,尽管在我看来这应该是从这个列表中检索地址

我怎么称呼这些数据有什么问题吗

import requests
from bs4 import BeautifulSoup

url = ("http://www.gym-directory.com/listing/bulldog-gym/")
r = requests.get(url)

soup = BeautifulSoup(r.content, "html.parser")
print soup.prettify()

listing_data = soup.find_all("div", {"class":"tab-contents"})
for item in listing_data:
    print item.contents[0].find_all("span",{"class":"wlt_shortcode_map_location"})[0].text

实际的问题很简单,但您还将发现其他问题。主要问题是,您要查找的第一个类是
选项卡内容
(no
s

然后,您将发现页面上有两个
选项卡内容
类。由于结构不同,第二次迭代会导致异常

由于页面中只使用了一种
wlt\u shortcode\u map\u location
,因此您可以简单地从顶层递归查找它

稍后您可能会发现编码问题-
r.content
是来自请求的原始字节,其中as
r.text
是使用服务器内容类型作为指南的解码字符串

考虑到所有这些,以下代码似乎可以满足您的需要:

url = ("http://www.gym-directory.com/listing/bulldog-gym/")
r = requests.get(url)

soup = BeautifulSoup(r.text, 'html.parser')

print soup.find("span",{"class":"wlt_shortcode_map_location"}).text 

祝你的编码好运

我猜你想做什么?清单中的数据是一个空列表,这就是为什么什么都不打印的原因。试试汤。找到所有的(“div”)并从那里探索。太棒了!谢谢你的简单易懂的解释。。我在Stack上的第一个问题和答案;)