使用 python 3 抓取需要登录的网站
Scraping a website with python 3 that requires login
只是关于一些抓取身份验证的问题。使用 BeautifulSoup
:
#importing the requests lib
import requests
from bs4 import BeautifulSoup
#specifying the page
page = requests.get("http://localhost:8080/login?from=%2F")
#parsing through the api
soup = BeautifulSoup(page.content, 'html.parser')
print(soup.prettify())
从这里输出,我认为很重要:
<table>
<tr>
<td>
User:
</td>
<td>
<input autocapitalize="off" autocorrect="off" id="j_username" name="j_username" type="text"/>
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<input name="j_password" type="password"/>
</td>
</tr>
<tr>
<td align="right">
<input id="remember_me" name="remember_me" type="checkbox"/>
</td>
<td>
<label for="remember_me">
Remember me on this computer
</label>
</td>
</tr>
</table>
这可以很好地抓取网站,但需要登录。我在这里使用 mechanicalsoup
库:
import mechanicalsoup
browser = mechanicalsoup.StatefulBrowser()
browser.open("http://localhost:8080/login?from=%2F")
browser.get_url()
browser.get_current_page()
browser.get_current_page().find_all('form')
browser["j_username"] = "admin"
browser ["j_password"] = "password"
browser.launch_browser()
但是它仍然不让我登录。
有没有人使用过 python3 的抓取工具来抓取具有身份验证的网站?
使用 MechanicalSoup,您首先需要指定要填写和提交的表单。如果您只有一种形式,请使用:
browser.select_form()
然后,填写表格后,您需要提交:
browser.submit_selected()
您可以阅读(新写的)MechanicalSoup tutorial or look at examples like logging in into GitHub with MechanicalSoup。
我看到你在使用请求。登录站点的语法如下:
import requests
page = requests.get("http://localhost:8080/login?from=%2F", auth=
('username', 'password'))
希望对您有所帮助!您可以在此处阅读有关身份验证的更多信息:http://docs.python-requests.org/en/master/user/authentication/
只是关于一些抓取身份验证的问题。使用 BeautifulSoup
:
#importing the requests lib
import requests
from bs4 import BeautifulSoup
#specifying the page
page = requests.get("http://localhost:8080/login?from=%2F")
#parsing through the api
soup = BeautifulSoup(page.content, 'html.parser')
print(soup.prettify())
从这里输出,我认为很重要:
<table>
<tr>
<td>
User:
</td>
<td>
<input autocapitalize="off" autocorrect="off" id="j_username" name="j_username" type="text"/>
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<input name="j_password" type="password"/>
</td>
</tr>
<tr>
<td align="right">
<input id="remember_me" name="remember_me" type="checkbox"/>
</td>
<td>
<label for="remember_me">
Remember me on this computer
</label>
</td>
</tr>
</table>
这可以很好地抓取网站,但需要登录。我在这里使用 mechanicalsoup
库:
import mechanicalsoup
browser = mechanicalsoup.StatefulBrowser()
browser.open("http://localhost:8080/login?from=%2F")
browser.get_url()
browser.get_current_page()
browser.get_current_page().find_all('form')
browser["j_username"] = "admin"
browser ["j_password"] = "password"
browser.launch_browser()
但是它仍然不让我登录。
有没有人使用过 python3 的抓取工具来抓取具有身份验证的网站?
使用 MechanicalSoup,您首先需要指定要填写和提交的表单。如果您只有一种形式,请使用:
browser.select_form()
然后,填写表格后,您需要提交:
browser.submit_selected()
您可以阅读(新写的)MechanicalSoup tutorial or look at examples like logging in into GitHub with MechanicalSoup。
我看到你在使用请求。登录站点的语法如下:
import requests
page = requests.get("http://localhost:8080/login?from=%2F", auth=
('username', 'password'))
希望对您有所帮助!您可以在此处阅读有关身份验证的更多信息:http://docs.python-requests.org/en/master/user/authentication/