python美汤爬着json
python beautiful soup crawling with json
我是 python 中 beautifulsoup 的新手,我正在尝试从网站中提取某些信息。深层链接和标题
我使用 beautifulsoup 提取 json 并得到了我的 beautifulsoup.beautifulsoup 变量汤。
但是我还没有提取出想要的信息。
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 欧元
Jetzt布臣
我想解析深层链接 (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'
欢迎任何反馈:)
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'})
你得到的 AttributeError 是因为你试图用 json 加载整个汤,这是不可能的。看起来您需要 <p>
标签的内容,然后您可以将其加载到 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')
我是 python 中 beautifulsoup 的新手,我正在尝试从网站中提取某些信息。深层链接和标题
我使用 beautifulsoup 提取 json 并得到了我的 beautifulsoup.beautifulsoup 变量汤。
但是我还没有提取出想要的信息。
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 欧元 Jetzt布臣
我想解析深层链接 (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'
欢迎任何反馈:)
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'})
你得到的 AttributeError 是因为你试图用 json 加载整个汤,这是不可能的。看起来您需要 <p>
标签的内容,然后您可以将其加载到 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')