【发布时间】:2017-03-23 11:17:15
【问题描述】:
我正在尝试获取电影的 href 链接(例如:在 IMDB 上搜索钢铁侠),但我似乎无法获取。运行代码时,我不断收到“无”,但如果我删除 .get('href'),代码将返回整行 html(包括我想要的链接)。我很感激这方面的任何帮助。谢谢!
from bs4 import BeautifulSoup
import requests
from urllib.parse import urljoin # For joining next page url with base url
search_terms = input("What movie do you want to know about?\n> ").split()
url = "http://www.imdb.com/find?ref_=nv_sr_fn&q=" + '+'.join(search_terms) + '&s=all'
def scrape_find_next_page(url):
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, "html.parser")
next_page = soup.find('td', 'result_text').get('href')
return next_page
next_page_url = scrape_find_next_page(url)
【问题讨论】:
-
看起来您获得的是
<td>元素的 href 而不是链接 -
我在任何地方都只看到过这个
for link in soup.findAll("a"): print link.get("href")
标签: python html beautifulsoup href imdb