用json爬行的python美丽汤

2024-07-04 09:10:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python中beautifulsoup的新手,我试图从一个网站中提取某些信息。deeplink和标题

我使用beauthulsoup来提取json并得到美丽之声。美丽之声可变汤。在

但我还没有设法提取出所需的信息。在

正在获取的HTML块:

<div class="activities-list horizontal">
<article data-href="http://www.getyourguide.de/london-l57/windsor-bath-und- stonehenge-tagesausflug-ab-london-t977/" id="t977" class="activity-card activity-card-horizontal
">
<div class="activity-card-content">
<a class="activity-card-link" href="http://www.getyourguide.de/london-l57/windsor-bath-und-stonehenge-tagesausflug-ab-london-t977/">
<div class="activity-card-image-container">
<img src="http://img.getyourguide.com/img/tour_img-206771-70.jpg" data- role="cover" alt="" />
</div>
<div class="activity-card-details">
<header class="activity-card-header">
<h3 class="activity-card-title">
Stonehenge, Windsor und Bath - Tagesausflug ab London
</h3>
<div class="activity-rating">
<span class="rating" title="Bewertung: 3,9 von 5">
<span class="rating-stars s30"></span>
<span class="rating-total">13 Bewertungen</span>
</span>                     </div>
</header>
<p class="activity-small-description">Verlassen Sie London und entdecken Sie    Reize der englischen Landschaft auf einer Ganztagestour, die Sie zu berühmten,   historischen Orten führt.…</p>
<div class="activity-info activity-duration">
<span class="activity-info-label activity-duration-label">

达乌尔: 10惊呆 Ab型 75欧元 杰茨·布肯 在

我想解析出deeplinks(href)和title(活动卡片标题)。到目前为止,我的逻辑是:

^{pr2}$

结果:

  newDictionary = json.loads(str(soup))['activities'].get("href")
  AttributeError: 'str' object has no attribute 'get'

欢迎任何反馈:)


Tags: divhttpimgabactivitycardclassheader
2条回答
response = urllib2.urlopen(link)
html = response.read()
soup = BeautifulSoup(html,'html.parser',from_encoding='utf-8')

对于deeplinks:

^{pr2}$

对于标题:

titles = soup.find_all('div',{'class':'activity-card-title'})

如果块中只有一个标题,则只使用“查找”

title  = soup.find('div',{'class':'activity-card-title'})

您得到的AttributeError是因为您试图用json加载整个soup,但这是做不到的。看起来您需要<p>标记的内容,然后可以将其加载到json中。你可以这样得到,像普通字典一样得到activities值。在

activities = json.loads(soup.find('p').text)['activities']

但是它变得有点奇怪,因为我们不再处理soup了,我们只是有一个看起来像html的大字符串。所以我们可以用它来做一个新的汤,并从得到的汤中得到深度链接和标题。在

^{pr2}$

相关问题 更多 >

    热门问题