我已经用 python 结合 selenium 编写了一个脚本,以从其着陆页抓取不同帖子的链接,最后通过跟踪指向其内页的 url 来获取每个帖子的标题。虽然我这里解析的内容是静态的,但是我用selenium看看它在multiprocessing中是如何工作的。
但是,我的意图是使用多处理进行抓取。到目前为止,我知道 selenium 不支持多处理,但看来我错了。
我的问题:当使用多处理运行时,如何减少使用 selenium 的执行时间?
This is my try (it's a working one)
:
import requests
from urllib.parse import urljoin
from multiprocessing.pool import ThreadPool
from bs4 import BeautifulSoup
from selenium import webdriver
def get_links(link):
res = requests.get(link)
soup = BeautifulSoup(res.text,"lxml")
titles = [urljoin(url,items.get("href")) for items in soup.select(".summary .question-hyperlink")]
return titles
def get_title(url):
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument("--headless")
driver = webdriver.Chrome(chrome_options=chromeOptions)
driver.get(url)
sauce = BeautifulSoup(driver.page_source,"lxml")
item = sauce.select_one("h1 a").text
print(item)
if __name__ == '__main__':
url = "https://stackoverflow.com/questions/tagged/web-scraping"
ThreadPool(5).map(get_title,get_links(url))
原文由 robots.txt 发布,翻译遵循 CC BY-SA 4.0 许可协议
在您的解决方案中,很多时间都花在了为每个 URL 启动 webdriver 上。您可以通过每个线程仅启动一次驱动程序来减少此时间:
在我的系统上,这将时间从 1 分钟 7 秒减少到仅 24.895 秒,提高了约 35%。要测试自己,请下载 完整的脚本。
注意:
ThreadPool
使用线程,受Python GIL约束。如果大部分任务受 I/O 限制,那也没关系。根据您对抓取结果进行的后处理,您可能需要使用multiprocessing.Pool
代替。这将启动并行进程,这些进程作为一个组不受 GIL 的约束。其余代码保持不变。