Я новичок в WebScraping/Python и BeautifulSoup, и мне трудно заставить работать мой код.Webscraping IMDb-страница с помощью BeautifulSoup
Я хотел бы, чтобы очистить URL: http://m.imdb.com/feature/bornondate ", чтобы получить:
- Название знаменитости
- знаменитости Image
- Профессия
- Лучшая работа
для десять знаменитостей на этой странице. Я не уверен, что я делаю неправильно.
Вот мой код:
import urllib2
from bs4 import BeautifulSoup
url = 'http://m.imdb.com/feature/bornondate'
test_url = urllib2.urlopen(url)
readHtml = test_url.read()
test_url.close()
soup = BeautifulSoup(readHtml)
# Using it track the number of Actor
count = 0
# Fetching the value present within tag results
person = soup.findChildren('section', 'posters list')
# Changing the person into an iterator
iterperson = iter(person[0].findChildren('a'))
# Finding 'a' in iterperson. Every 'a' tag contains information of a person
for a in iterperson:
imgSource = a.find('img')['src'].split('._V1.')[0] + '._V1_SX214_AL_.jpg'
person = a.findChildren('div', 'label')
title = person[0].find('span', 'title').contents[0]
##profession = person[0].find('div', 'detail').contents[0].split(,)
##bestWork = person[0].find('div', 'detail').contents[1].split(,)
print '*******************************IMDB People Born Today***********************************'
# Printing the S.No of the person
print 'S.No. --> ',
count += 1
print count
# Printing the title/name of the person
print 'Title --> ' + title
# Printing the Image Source of the person
print 'Image Source --> ', imgSource
# Printing the Profession of the person
##print 'Profession --> ', profession
# Printing the Best work of the person
##print 'Best Work --> ', bestWork
В настоящее время ничего не выходит на печать. Кроме того, если это смутно, вы могли бы объяснить, как сделать только имя знаменитости, например?
Вот HTML код первой знаменитости, если это помогает:
<section class="posters list">
<h1>March 7</h1>
<a href="/name/nm0186505/" class="poster "><img src="http://ia.media-imdb.com/images/M/[email protected]_V1._CR0,0,1369,2019_SX40_SY59.jpg" style="background:url('http://i.media-imdb.com/images/mobile/people-40x59-fade.png')" width="40" height="59"><div class="label"><span class="title">Bryan Cranston</span><div class="detail">Actor, "Ozymandias"</div></div></a>
Thankyou, было бы невозможно сделать это в beautifulsoup? –
@PatrickLee да, если вы хотите, вы можете передать page_source из 'selenium' в' BeautifulSoup': после загрузки плакатов выполните: 'soup = BeautifulSoup (driver.page_source)'. – alecxe
Я обязательно попробую selenium, но, к сожалению, для этой проблемы мне нужно использовать BeautifulSoup самостоятельно, поскольку это инструмент, который я ожидаю использовать :( –