【问题标题】:Scraping a webpage taking too long using Selenium, BeautifulSoup使用 Selenium、BeautifulSoup 抓取网页耗时过长
【发布时间】:2020-04-21 23:00:30
【问题描述】:

我想抓取一个网站及其子页面,但耗时太长。如何优化请求或使用替代解决方案?

下面是我正在使用的代码。加载谷歌主页需要 10 秒。所以如果我给它 280 个链接,它显然是不可扩展的

from selenium import webdriver
import time
# prepare the option for the chrome driver
options = webdriver.ChromeOptions()
options.add_argument('headless')

# start chrome browser
browser = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver" ,chrome_options=options)
start=time.time()
browser.get('http://www.google.com/xhtml')
print(time.time()-start)
browser.quit()

【问题讨论】:

标签: python html selenium beautifulsoup web-crawler


【解决方案1】:

使用 python requestsBeautiful soup 模块。

import requests
from bs4 import BeautifulSoup
url="https://tajinequiparle.com/dictionnaire-francais-arabe-marocain/"
url1="https://tajinequiparle.com/dictionnaire-francais-arabe-marocain/{}/"
req = requests.get(url,verify=False)
soup = BeautifulSoup(req.text, 'html.parser')
print("Letters : A")
print([item['href'] for item in soup.select('.columns-list a[href]')])

letters=['B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']

for letter in letters:

    req = requests.get(url1.format(letter), verify=False)
    soup = BeautifulSoup(req.text, 'html.parser')
    print('Letters : ' + letter)
    print([item['href'] for item in soup.select('.columns-list a[href]')])

【讨论】:

    【解决方案2】:

    您可以使用该脚本来提高速度。多线程爬虫胜过一切:

    https://edmundmartin.com/multi-threaded-crawler-in-python/

    之后您必须更改该代码:

    def run_scraper(self):
        with open("francais-arabe-marocain.csv", 'a') as file:
            file.write("url")
            file.writelines("\n")
            for i in range(50000):
                try:
                    target_url = self.to_crawl.get(timeout=600)
                    if target_url not in self.scraped_pages and "francais-arabe-marocain" in target_url:
                        self.scraped_pages.add(target_url)
                        job = self.pool.submit(self.scrape_page, target_url)
                        job.add_done_callback(self.post_scrape_callback)
                        df = pd.DataFrame([{'url': target_url}])
                        df.to_csv(file, index=False, header=False)
                        print(target_url)
                except Empty:
                    return
                except Exception as e:
                    print(e)
                    continue
    

    如果网址包含“francais-arabe-marocain”,则将网址保存在 csv 文件中。

    之后,您可以用相同的方式逐行读取 csv 的一个循环中的 url

    【讨论】:

      【解决方案3】:

      尝试像这样使用 urllib

      import urllib.request
      start=time.time()
      page = urllib.request.urlopen("https://google.com/xhtml")
      print(time.time()-start)
      

      只用了 2 秒。但是,这也取决于您的连接质量

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-23
        • 2020-09-13
        • 1970-01-01
        • 2013-06-30
        • 1970-01-01
        • 2022-11-07
        • 2018-09-21
        • 2020-09-17
        相关资源
        最近更新 更多