【问题标题】:python beautiful soup crawling with json用json爬行的python美汤
【发布时间】:2016-01-07 05:18:58
【问题描述】:

我是 python 中的 beautifulsoup 新手,我正在尝试从网站中提取某些信息。深层链接和标题

我用beautifulsoup提取json,得到了我的beautifulsoup.beautifulsoup变量soup。

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

正在收获的 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 震惊 抗体 75 欧元 杰茨特布臣

我想解析出深层链接 (href) 和标题 (activity-card-title)。到目前为止,这是我的逻辑:

 response = urlopen("http://www.getyourguide.de/s/search.json?    q=London&page=8")
 content = response.read()
 soup = BeautifulSoup(content)
 newDictionary = json.loads(str(soup))['activities'].get("href")
 print(newDictionary)

结果:

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

感谢任何反馈:)

【问题讨论】:

    标签: python html json request beautifulsoup


    【解决方案1】:
    response = urllib2.urlopen(link)
    html = response.read()
    soup = BeautifulSoup(html,'html.parser',from_encoding='utf-8')
    

    对于深层链接:

    links = soup.find_all('a',href=True)
    

    标题:

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

    如果块中只有 1 个标题,则仅使用查找

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

    【讨论】:

    • 还是不行。得到一个空的结果 []。有什么想法吗?
    【解决方案2】:

    你得到的 AttributeError 是因为你试图用 json 加载整个汤,这是无法完成的。看起来您想要&lt;p&gt; 标记的内容,然后您可以将其加载到 json 中。你可以像这样得到,像普通字典一样得到活动值。

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

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

    newsoup = BeautifulSoup(activities)
    
    links = newsoup.find_all('a', href=True)
    deeplinks = [ a['href'] for a in links ]
    
    titles = newsoup.find_all('h3', 'activity-card-title')
    

    【讨论】:

      猜你喜欢
      • 2017-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-11
      • 2020-07-01
      • 2021-12-15
      • 2022-08-19
      • 1970-01-01
      相关资源
      最近更新 更多