使用 Beautifullsoup 在 python 中抓取 Reelgood.com

时间:2021-03-22 11:20:49

标签: python web-scraping beautifulsoup

我正在尝试为网站 ReelGood.com 构建一个抓取工具(使用 Python)。

如果我在 Reelgood 上观看特定的电影,它会向我显示这样的播放按钮: Stream button

如果我点击那个按钮,它会将我重定向到例如 https://www.netflix.com/title/70232180 现在我想抓取那个特定的 URL。所以我想我制作了一个小的 python 脚本来抓取所有包含 https://netflix.com/x 的链接。

所以我想出了这个:

from bs4 import BeautifulSoup
import requests

URL = "https://reelgood.com/movie/the-intouchables-2011"
page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")
for a_href in soup.find_all("a", href=True):
    print(a_href["href"])

现在这确实给了我所有链接的打印,但没有包含我重定向到的 url 的链接。

有人知道如何吐出 Netflix.com 网址吗?

2 个答案:

答案 0 :(得分:1)

要过滤仅包含 https://www.netflix.com/ 的链接,您可以使用 CSS 选择器:a[href*="https://www.netflix.com/"],它将选择所有 a,其中 href 包含 {{1} }.

https://www.netflix.com/

输出:

from bs4 import BeautifulSoup
import requests

URL = "https://reelgood.com/movie/the-intouchables-2011"
page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")

for a_href in soup.select('a[href*="https://www.netflix.com/"]'):
    print(a_href["href"])

答案 1 :(得分:0)

我认为您可以使用我的代码将其添加到您的需求中
从硒导入网络驱动程序 从 selenium.webdriver.chrome.options 导入选项 从 selenium.webdriver.support.ui 导入 WebDriverWait 导入时间 从 geopy.geocoders 导入 Nominatim 导入时间 从 pprint 导入 pprint

# instantiate a new Nominatim client
app = Nominatim(user_agent="tutorial")

def getLocation():
    #autoriser le naviagateur pour acceder à l'emplacement actuelle par defaut,
    # Si on essaye d'accéder à un site Web : « https://mycurrentlocation.net » via chrome,
    # il demande d'autoriser l'accès à la localisation. La commande « - use-fake-ui-for-media-stream »
    # accordera toutes les autorisations pour l'emplacement, le microphone, etc. automatiquement.
    options = Options()
    options.add_argument("--use-fake-ui-for-media-stream")
    # appelez la page Web https://mycurrentlocation.net/ et attendez 20 secondes que la page se charge.
    timeout = 20
    #Pour chromedriver il faut avoir la meme version que google chrome
    driver = webdriver.Chrome(executable_path = './chromedriver.exe', chrome_options=options)
    #driver = webdriver.Chrome(executable_path = './chromedriver.exe', chrome_options=options) -> on peut mettre ça a la palce
    driver.get("https://mycurrentlocation.net/")
    wait = WebDriverWait(driver, timeout)
    time.sleep(3)
    #Trouvez le XPath des éléments de latitude et de longitude mentionnés sur la page Web puis copier le nom de la classe qu'on souhaite récupérer
    neighborhood = driver.find_elements_by_xpath('//*[@id="neighborhood"]')
    neighborhood = [x.text for x in neighborhood]
    neighborhood = str(neighborhood[0])

    regionname = driver.find_elements_by_xpath('//*[@id="regionname"]')
    regionname = [x.text for x in regionname]
    regionname = str(regionname[0])

    placename = driver.find_elements_by_xpath('//*[@id="placename"]')
    placename = [x.text for x in placename]
    placename = str(placename[0])

    driver.quit()
    return (neighborhood,regionname,placename)

neighborhood,regionname,placename=getLocation()

print("le résultat est : \n ",
    neighborhood,regionname,placename)