我正在尝试创建一个脚本,我可以在不被封锁的情况下解析网站上的几个字段。我希望获得数据的网站需要凭据才能访问它的内容。如果不是为了登录,我可以使用代理的旋转绕过速率限制。
当我从一个基于登录的网站上抓取内容时,我试图找出避免被该网站禁止的任何方法,同时从那里抓取数据。To be specific, my script currently can fetch content from that site flawlessly but my ip address gets banned along the way if I keep on scraping。
到目前为止,我已经写了(认为下面的站点地址是占位符):
import requests
from bs4 import BeautifulSoup
url = "https://stackoverflow.com/users/login?ssrc=head&returnurl=https%3a%2f%2fstackoverflow.com%2f"
with requests.Session() as s:
s.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36'
req = s.get(url)
payload = {
"fkey": BeautifulSoup(req.text,"lxml").select_one("[name='fkey']")["value"],
"email": "some email",
"password": "some password",
}
res = s.post(url,data=payload)
soup = BeautifulSoup(res.text,"lxml")
for post_title in soup.select(".summary > h3 > a.question-hyperlink"):
print(post_title.text)如何避免在从基于登录的站点抓取数据时被禁止?
发布于 2021-03-09 20:19:15
使用selenium web驱动程序打开浏览器并加载所需的页面,然后可以在运行的浏览器上执行刮除。这是一种站点不阻止您的方法,因为它包含模拟用户与站点交互的标头等。
例如,使用铬网
self.driver = webdriver.Chrome(executable_path=path +/driver//chromedriver.exe', chrome_options=self.options) self.driver.get('site to scrape')
这将打开一个浏览器,然后在此浏览器上擦除,检查这个硒蟒。
https://stackoverflow.com/questions/66413511
复制相似问题