2017-07-26 5 views
2

これに問題があります。私は単一のimgを表示する方法についてはわかりません。たとえば:イメージを取得する単一のイメージ名を表示するURL

<img srcset="http://i4.manchestereveningnews.co.uk/incoming/article13390833.ece/ALTERNATES/s180/Mike-Grimshaw-34-was-fatally-attacked-following-the-attack-outside-his-Trafford-home-last-Thursday.jpg 180w, http://i4.manchestereveningnews.co.uk/incoming/article13390833.ece/ALTERNATES/s390/Mike-Grimshaw-34-was-fatally-attacked-following-the-attack-outside-his-Trafford-home-last-Thursday.jpg 390w, http://i4.manchestereveningnews.co.uk/incoming/article13390833.ece/ALTERNATES/s458/Mike-Grimshaw-34-was-fatally-attacked-following-the-attack-outside-his-Trafford-home-last-Thursday.jpg 458w" src="http://i4.manchestereveningnews.co.uk/incoming/article13390833.ece/ALTERNATES/s615/Mike-Grimshaw-34-was-fatally-attacked-following-the-attack-outside-his-Trafford-home-last-Thursday.jpg"> 

あなたが上見ることができるように、さまざまな代替画像があるが、しかし、私は示すべき単一のものをこすりしようとしています。しかし、私は、単一の画像のみを表示するようにしようとしています、結果は

Greater Manchester News 
<link href="rss.xml" rel="alternate" title="Default home feed" 

type="application/rss+xml"/> 

<img data-`src="http://i4.manchestereveningnews.co.uk/incoming/article13390833.ece/ALTERNA`TES/s615/Mike-Grimshaw-34-was-fatally-attacked-following-the-attack-outside-his-Trafford-home-last-Thursday.jpg" data-`srcset="http://i4.manchestereveningnews.co.uk/incoming/article13390833.ece/ALTE`RNATES/s180/Mike-Grimshaw-34-was-fatally-attacked-following-the-attack-outside-his-Trafford-home-last-Thursday.jpg 180w,` http://i4.manchestereveningnews.co.uk/incoming/article13390833.ece/ALT`ERNATES/s 

390/Mike-Grimshaw-34-was-fatally-attacked-following-the-attack-outside-his-`Trafford-home-last-Thursday.jpg 390w, `http://i4.manchestereveningnews.co.uk/incoming/article13390833.ece/ALTERNATES/s458/Mike-Grimshaw-34-was-fatally-attacked-following-t`he-attack-outs`ide-his- 

Trafford-home-last-Thursday.jpg 458w"/> 
     Family of dad stabbed in the neck while defendin 

g his fiancée from thugs speak of their heartbreak 
     Mike Grimshaw, 34, died after being stabbed in the neck outside his 

home in Trafford last Thursday 

Trafford 

、複数の画像の名前を示しています

import bs4 as bs 
import urllib.request 
import datetime 
import random 
import re 


random.seed(datetime.datetime.now()) 

sauce = urllib.request.urlopen('http://www.manchestereveningnews.co.uk/news/greater-manchester-news').read() 
soup = bs.BeautifulSoup(sauce, 'lxml') 

# 




title = soup.title 
link = soup.link 
image = re.search(img 'srcset=img(.*?),) 
#this doesnt work, not sure how to 

strong = soup.strong 
description = soup.description 
location = soup.location 


title = soup.find('h1', class_ ='publication-font',) 

image = soup.find('img') 
strong = soup.find('strong') 
location = soup.find('em').find('a') 
description = soup.find('div', class_='description',to.text) 


#Previous Code 
print("H1:", title.text) 
print("Article Link:", link) 
print("Image Url:\n", image) 
print("1st Paragraph:\n", strong.text) 
print("2nd Paragraph:\n", description.string) 
print("Location:\n", location.text) 

私のコードは、上記の私の以前の試行で表示されるでしょうが、以前の結果であり、リンク。私はこれをどうやって行うのですか?

アイデアをいただければ幸いです。

答えて

0

あなたが欲しい画像を取得する属性data-srcまたはdata-srcsetにアクセスすることができます。そして、あなたはimg_setであなたが好きなインデックスにアクセスすることができます

image = soup.find('img') 
single_img = image.get('data-src') # return the main image link 

または

import re 
image = soup.find('img') 
img_string = image.get('data-srcset') # this return a string you have to parse 
img_set = re.findall(r'(https?://[^\s]+)', img_set) # regex to match only links 

を(ちょうどの長さをテストします前のリスト)

関連する問題