簡體   English   中英

使用 webbot 抓取網頁

[英]Web Scraping using webbot

我正在嘗試創建一個簡單的程序來使用我的憑據登錄網頁並獲取我在大學帳戶中剩余的彈性美元總額。 從登錄頁面開始,我登錄,然后重定向到感興趣的頁面,我只想獲取該金額並對其進行一些操作。

我目前正在使用 webbot 作為登錄部分,這很有效,我剛剛編輯了憑據:

from webbot import Browser

web = Browser()
web.go_to('insert my url here')
#enter your username and password in the into fields below
web.type('insert email here', into='username')
web.type('insert password here', into='password')
web.click('Login', tag='span')

到目前為止,這非常有效,創建了一個 Chrome 實例並登錄到我想從中獲取美元金額的頁面。 我想我可能想繼續使用 urllib,但是,我認為 urllib 不會從我當前登錄的 Chrome 實例中受益。 我該如何解決這個問題並從頁面中獲取一個簡單的 html 元素?

您首先需要獲取當前網頁的 html 源代碼。 您可以使用get_page_source()做到這get_page_source() 然后你需要將html源代碼傳遞給beautifulsoup

from webbot import Browser
from bs4 import BeautifulSoup
import time

web = Browser()
web.go_to('insert my url here')
#enter your username and password in the into fields below
web.type('insert email here', into='username')
web.type('insert password here', into='password')
web.click('Login', tag='span')
time.sleep(5)

content = web.get_page_source()
soup = BeautifulSoup(content)

#You can now find the element you want
samples = soup.find_all("a", "item-title")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM