Ghu*_*ing 6 python selenium multithreading web-scraping python-3.x
我有一个无法为其开发逻辑的用例。将其浮动在这里以获取专家的建议。
快速上下文:
我有 2,500 个 URL 的列表。我可以使用 Python 和 Selenium 依次抓取它们。
1,000 个 URL 的运行时间约为 1.5 小时
我想要实现的目标:
我试图通过并行执行来优化运行时间。我查看了有关堆栈溢出的各种帖子。不知怎的,我无法找到拼图中缺失的部分。
细节
我需要重用驱动程序,而不是为每个 URL 关闭并重新打开它们。我遇到了一个利用 threading.local() 的Python selenium 多重处理。如果我重新运行相同的代码,打开的驱动程序数量会以某种方式超过指定的线程数量
请注意,该网站要求用户使用用户名和密码登录。我的目标是第一次启动驱动程序(例如 5 个驱动程序)并登录。我想继续对所有未来的 URL 重复使用相同的驱动程序,而无需关闭驱动程序并再次登录
另外,我是 Selenium 网络抓取的新手。只是熟悉基础知识。多线程是未知领域。我非常感谢你在这里的帮助
下面分享我的代码片段:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import pandas as pd
from multiprocessing.dummy import Pool as ThreadPool
threadLocal = threading.local()
# Function to open web driver
def get_driver():
options = Options()
driver = webdriver.Chrome(<Location to chrome driver>, options = options)
return driver
# Function to login to website & scrape from website
def parse_url(url):
driver = get_driver()
login_url = "https://..."
driver.get(login_url)
# Enter user ID
# Enter password
# Click on Login button
# Open web page of interest & scrape
driver.get(url)
htmltext = driver.page_source
htmltext1 = htmltext[0:100]
return [url, htmltext1]
# Function for multi-threading
def main():
urls = ["url1",
"url2",
"url3",
"url4"]
pool = ThreadPool(2)
records = pool.map(parse_url, urls)
pool.close()
pool.join()
return records
if __name__ =="__main__":
result = pd.DataFrame(columns = ["url", "html_text"], data = main())
Run Code Online (Sandbox Code Playgroud)
我怎样才能修改上面的代码,使得:
我相信在单独的进程中启动浏览器并通过队列与他通信是一个很好的方法(并且更具可扩展性)。如果出现问题,进程可以很容易地被终止并重新生成。伪代码可能如下所示:
# worker.py
def entrypoint(in_queue, out_queue): # run in process
crawler = Crawler()
browser = Browser() # init, login and etc.
while not stop:
command = in_queue.get()
result = crawler.handle(command, browser)
out_queue.put(result)
Run Code Online (Sandbox Code Playgroud)
# main.py
import worker
in_queue, out_queue = create_queues()
create_process(worker.entrypoint, args=(in_queue, out_queue))
while not stop:
in_queue.put(new_task)
result = out_queue.get()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3070 次 |
| 最近记录: |