【发布时间】:2020-09-19 06:19:39
【问题描述】:
我正在尝试从 IMDB 电影页面中抓取演员/女演员 ID。我只想要演员(我不想得到任何剧组),而这个问题是专门关于获取人的内部ID的。我已经有了人们的名字,所以我不需要帮助来获取这些名字。我从这个网页 (https://www.imdb.com/title/tt0084726/fullcredits?ref_=tt_cl_sm#cast) 开始作为硬编码的 url 来获取正确的代码。
在检查链接时,我发现演员的链接看起来像这样。
<a href="/name/nm0000638/?ref_=ttfc_fc_cl_t1"> William Shatner</a>
<a href="/name/nm0000559/?ref_=ttfc_fc_cl_t2"> Leonard Nimoy</a>
<a href="/name/nm0346415/?ref_=ttfc_fc_cl_t17"> Nicholas Guest</a>
而其他贡献者的看起来像这样
<a href="/name/nm0583292/?ref_=ttfc_fc_dr1"> Nicholas Meyer </a>
<a href="/name/nm0734472/?ref_=ttfc_fc_wr1"> Gene Roddenberry</a>
这应该允许我通过检查 href 的结尾是否为“t[0-9]+$”而不是相同但带有“dr”或“wr”来区分演员/女演员和导演或作家等剧组”。
这是我正在运行的代码。
import urllib.request
from bs4 import BeautifulSoup
import re
movieNumber = 'tt0084726'
url = 'https://www.imdb.com/title/' + movieNumber + '/fullcredits?ref_=tt_cl_sm#cast'
def clearLists(n):
return [[] for _ in range(n)]
def getSoupObject(urlInput):
page = urllib.request.urlopen(urlInput).read()
soup = BeautifulSoup(page, features="html.parser")
return(soup)
def getPeopleForMovie(soupObject):
listOfPeopleNames, listOfPeopleIDs, listOfMovieIDs = clearLists(3)
#get all the tags with links in them
link_tags = soupObject.find_all('a')
#get the ids of people
for linkTag in link_tags:
link = str(linkTag.get('href'))
#print(link)
p = re.compile('t[0-9]+$')
q = p.search(link)
if link.startswith('/name/') and q != None:
id = link[6:15]
#print(id)
listOfPeopleIDs.append(id)
#return the names and IDs
return listOfPeopleNames, listOfPeopleIDs
newSoupObject = getSoupObject(url)
pNames, pIds = getPeopleForMovie(newSoupObject)
上面的代码返回一个空的 ID 列表,如果你取消注释 print 语句,你可以看到这是因为放入“link”变量的值最终是下面的值(特定人的变化)
/name/nm0583292/
/name/nm0000638/
那不行。我只想要演员和女演员的 ID,以便以后可以使用这些 ID。 我试图在stackoverflow上找到其他答案;我一直没能找到这个特定的问题。
这个问题 (Beautifulsoup: parsing html – get part of href) 与我想要做的很接近,但它从标签之间的文本部分获取信息,而不是从标签属性中的 href 部分获取信息。
如何确保从页面中只获得我想要的名称 ID(仅是演员的 ID)? (另外,请随时提供收紧代码的建议)
【问题讨论】:
-
有一些关于代码的 cmets,但最重要的是,您的代码加载的 html 与浏览器中呈现的 html 不匹配 - 它不包括您尝试匹配的查询参数,所以
/name/nm0000638/?ref_=ttfc_fc_cl_t1看起来就像/name/nm0000638/。您可能需要考虑另一种匹配演员的方式,例如仅在演员部分中获取链接? BS 应该让它变得相当简单。
标签: python html web-scraping beautifulsoup href