[英]Unable to scrape a piece of static information from a webpage
我已經在Python創建腳本中使用憑證登錄網頁,然后分析了條信息SIGN OUT
從另一個鏈接(該腳本應該重定向到該鏈接),以確保我沒有登錄。
我嘗試過:
import requests
from bs4 import BeautifulSoup
url = "https://member.angieslist.com/gateway/platform/v1/session/login"
link = "https://member.angieslist.com/"
payload = {"identifier":"usename","token":"password"}
with requests.Session() as s:
s.post(url,json=payload,headers={
"User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36",
"Referer":"https://member.angieslist.com/member/login",
"content-type":"application/json"
})
r = s.get(link,headers={"User-Agent":"Mozilla/5.0"},allow_redirects=True)
soup = BeautifulSoup(r.text,"lxml")
login_stat = soup.select_one("button[class*='menu-item--account']").text
print(login_stat)
當我運行上面的腳本,我得到AttributeError: 'NoneType' object has no attribute 'text'
這個錯誤,這意味着我去什么地方錯了我的登錄過程,我想分析的信息SIGN OUT
是一個靜態的內容。
我如何解析這個SIGN OUT
從網頁信息?
該網站需要使用JavaScript。 盡管您可以通過登錄API正確生成登錄令牌,但是當您轉到主頁時,它將進行多個其他API調用,然后更新頁面。
因此,問題與登錄不起作用無關。 您需要為此使用諸如硒之類的東西
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://member.angieslist.com/member/login")
driver.find_element_by_name("email").send_keys("none@getnada.com")
driver.find_element_by_name("password").send_keys("NUN@123456")
driver.find_element_by_id("login--login-button").click()
import time
time.sleep(3)
soup = BeautifulSoup(driver.page_source,"lxml")
login_stat = soup.select("[id*='menu-item']")
for item in login_stat:
print(item.text)
print(login_stat)
driver.quit()
我在這里混合了bs4
和selenium
以便於您使用,但是如果您願意,也可以只使用selenium
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.